【问题标题】:How to convert url segment to querystring with Nginx rewrite如何使用 Nginx 重写将 url 段转换为查询字符串
【发布时间】:2017-05-11 05:02:26
【问题描述】:

我想要这个简单的重写规则:

http://example.com/8743b52063cd84097a65d1633f5c74f5?param1=999&param2=2222

被重定向到:

http://example.com/index.php?param1=999&param2=2222&hash=8743b52063cd84097a65d1633f5c74f5

以下是我的默认位置:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

如何使用 Nginx 重写来实现这一点?

【问题讨论】:

    标签: nginx url-rewriting


    【解决方案1】:

    使用rewrite statement

    rewrite "^/(\w{32})$" /index.php?hash=$1 last;
    

    或者,在location block 内:

    location ~ "^/(?<hash>\w{32})$" {
        rewrite ^ /index.php?hash=$hash last;
    }
    

    【讨论】:

    • 谢谢@Richard!!那行得通!如果无论有多少字符,我都想获取整个片段怎么办?
    • 您可能想要设置最小和最大限制,否则其他 URI 可能开始看起来像一个有效的哈希。但是\w{16,32} 将匹配例如 16 到 32 个字符之间的字母数字序列。请参阅quick reference here
    • 好的,我理解理查德,这是有道理的。我看一下参考资料。感谢您的支持!
    猜你喜欢
    • 1970-01-01
    • 2012-10-08
    • 2021-09-17
    • 1970-01-01
    • 2010-10-27
    • 2014-07-23
    • 2021-01-06
    • 1970-01-01
    • 2011-02-18
    相关资源
    最近更新 更多