简体   繁体   中英

Nginx - Rewrite using last url segment

My original url are like following:

http://example.com/lab/file.php?id=4&idl=42

I want to beautify it, making it also accessible through a url like following:

http://example.com/l/not/important/url/part/?id=4&idl=42

I tried with following code, but it doesn't work.

rewrite /l/.*/([^/]+)/?$ /lab/file.php$1 last;

How to solve?

Anything after the ? is the query string and cannot be rewritten using the rewrite directive. However, by default, rewrite will append the original query string anyway.

You can either rewrite any URI that begins with /l/ or create a location for /l/ , for example:

rewrite ^/l/ /lab/file.php last;

or:

location ^~ /l/ {
    rewrite ^ /lab/file.php last;
}

The ^~ modifier makes this prefix location take precedence over regex locations at the same level. See this document for details.

In both cases, rewrite will append the original query string to the rewritten URI. See this document for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM