【问题标题】:What's the correct nginx rewrite expression for this URL pattern?此 URL 模式的正确 nginx 重写表达式是什么?
【发布时间】:2016-10-10 11:17:48
【问题描述】:

我正在努力让 Nginx 重写规则起作用。重写模式应匹配的示例 URL 是:

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

我的重写规则:

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

但这会返回带有示例 URL 的 404。

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

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

非常感谢任何帮助!

【问题讨论】:

    标签: nginx url-rewriting


    【解决方案1】:

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

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

    你可以实现类似的东西:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多