【问题标题】:Tricky nginx redirect rules with folders and subfolders带有文件夹和子文件夹的棘手 nginx 重定向规则
【发布时间】:2018-05-11 06:36:35
【问题描述】:

我想用 nginx 实现这样的 301 重定向:

https://www.example.com/foo1/bar1 -> https://www.example.com/foo2/bar2 
https://www.example.com/foo1/bar3 -> https://www.example.com/foo2/bar4 
https://www.example.com/foo1/whatever -> https://www.example.com/foo2/whatever 
https://www.example.com/foo1?argumet -> https://www.example.com/foo2?argument 

为了尝试并完成这个,我正在尝试使用:

#Redirect the longest forced links first
location ~ ^/foo1/bar1 {
    return 301 https://www.example.com/foo2/bar2;
    break;
}
location ~ ^/foo1/bar3 {
    return 301 https://www.example.com/foo2/bar4;
    break;
}
#Redirect everything else except for the matches above according to the rule
location ~ ^/foo1(?:/(.*))?$ {
    return 301 https://www.example.com/foo2/$1$is_args$args;
}

前三种情况都很好。但是,对于最后一种情况,我会重定向到:

https://www.example.com/foo2/?argument

而不是

https://www.example.com/foo2?argument

...它破坏了网站的逻辑。

你能帮我解决这个问题吗? 谢谢!

【问题讨论】:

    标签: redirect nginx return http-status-code-301


    【解决方案1】:

    试试:

    location ~ ^/foo1(/.*)?$ {
        return 301 /foo2$1$is_args$args;
    }
    

    【讨论】:

    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2017-03-09
    • 2014-09-02
    • 1970-01-01
    • 2018-09-04
    • 2013-03-13
    相关资源
    最近更新 更多