【问题标题】:nginx location regexp for query string用于查询字符串的 nginx 位置正则表达式
【发布时间】:2012-07-03 14:04:08
【问题描述】:

我想将使用 perticuler 查询字符串直接到达我的服务器的任何内容重定向到同一域中的其他位置。

如果用户来

http://www.mydomain.com/?abc=js9sd70s

我想把它重定向到

http://www.mydomain.com/otherpath/?abc=js9sd70s

查询字符串?abc=js9sd70s应与新网址相同。

请推荐 nginx 配置文件。

我已经尝试了以下代码的大部分替代方案。通过保留'\'之前?和 = 符号。

        location ~ /?abc=.* {
                rewrite ^/(.*)$ http://www.mydomain.com/otherpath/$1 permanent;
        }

请建议我更改此位置。

【问题讨论】:

    标签: redirect url-rewriting nginx webserver


    【解决方案1】:

    简短回答,试试这个配置:

    location = / {
        if ( $arg_abc ) {
            rewrite ^ /otherpath/ permanent;
        }
    }
    

    【讨论】:

    • 是的,它将www.mydomain.com/?abc=js9sd70s 重定向到www.mydomain.com/otherpath/?abc=js9sd70s,但现在它正试图一次又一次地重定向www.mydomain.com/otherpath/?abc=js9sd70s,但它没有完成。感谢您的帮助
    • 我可以特别检查?abc 是否在域根目录下,那么只有这个重定向规则适用?就像在这种情况下 www.mydomain.com/?abc=js9sd70s 应该重定向但 www.mydomain.com/otherpath/?abc=js9sd70s 不应该。
    • 上述规则仅在域根中有效,使用:location = / { 文档:wiki.nginx.org/HttpCoreModule#location
    • 好的,我需要在 nginx 配置文件中将 location /otherpath/location / 分开吗?到目前为止,我在配置中只有一段 location / { #rewrite code }
    • 拜托!小心点! location / 和 location = / 这是不同的位置!先阅读文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2017-04-11
    相关资源
    最近更新 更多