使用return 301

时间:2017-11-15 07:26:30

标签: nginx nginx-location

我需要从网址进行永久重定向:

https://SiteName.com/shop/forklift-tires/XXX/
to
https://SiteName.com/shop/tires/forklift-tires/XXX/

https://SiteName.com/shop/forklift-tires/YYY/
to
https://SiteName.com/shop/tires/forklift-tires/YYY/

我怎么能用下面的结构来做呢?

location *something* {
        return 301 *something*;
}

正如我从文章"Pitfalls and Common Mistakes"所理解的那样,最好使用返回而不是重写

1 个答案:

答案 0 :(得分:5)

更新(正确答案)

继续发表评论,试试这个:

location  ~ ^shop/forklift-tires/(.*) {
    return 301  /shop/tires/forklift-tires/$1 ;
}

以前的回答(抱歉误解了这个问题)

创建另一个服务器指令,如下所示:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name Sitename.com;
    return 301 https://$host$request_uri;
}

http://SiteName.com/ *的所有流量都将重定向到https://SiteName.com/ *。