Tricky nginx重定向规则与文件夹和子文件夹

时间:2017-11-27 22:21:09

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

我想用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;
}

这对前3种情况都有好处。但是,对于最后一种情况,我正在重定向:

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

而不是

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

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

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

1 个答案:

答案 0 :(得分:1)

尝试:

{{1}}