这个URL模式的正确nginx重写表达式是什么?

时间:2016-10-10 11:17:49

标签: nginx url-rewriting

我要香蕉试图让Nginx重写规则起作用。重写模式应匹配的示例URL是:

https://test.mydomaind.com/abc.php?id=1

我的重写规则:

rewrite ^/abc\.php?id=(.*)$ https://test.mydomain.com/page/$1 permanent;

但是这会返回带有示例网址的404。

谁能告诉我我做错了什么?当我在条件中省略查询字符串参数时,重写确实有效并且用户被重定向301:

rewrite ^/abc\.php$ https://test.mydomain.com/page permanent; # THIS WORKS

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

?以后的任何内容都是查询字符串,在将URI与locationsrewrite语句匹配时不予考虑。

可以在$request_uri$query_string(又名$args)变量以及前缀为$arg_的变量系列中访问查询字符串(或参数)。

你可以用以下方法实现类似的东西:

location = /abc.php {
    return 301 https://test.mydomain.com/page/$arg_id;
}

如果您需要测试是否存在id=参数,则需要使用mapif语句。