Nginx - 使用最后一个网址段重写

时间:2016-07-21 15:42:02

标签: regex nginx url-rewriting

我原来的网址如下:

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

我想美化它,也可以通过以下网址访问它:

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

我尝试使用以下代码,但它无法正常工作。

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

如何解决?

1 个答案:

答案 0 :(得分:1)

?之后的任何内容都是查询字符串,无法使用rewrite指令重写。但是,默认情况下,rewrite无论如何都会附加原始查询字符串。

您可以重写以/l/开头的任何URI,也可以为/l/创建一个位置,例如:

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

或:

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

^~修饰符使此前缀位置优先于同一级别的正则表达式位置。有关详细信息,请参阅this document

在这两种情况下,rewrite都会将原始查询字符串附加到重写的URI。有关详细信息,请参阅this document

相关问题