【问题标题】:Nginx 301 redirect syntax errorNginx 301 重定向语法错误
【发布时间】:2016-11-06 08:39:51
【问题描述】:

刚刚发现我的nginx语法不正确:

location /news { rewrite ^(.*)$ /blog redirect;}

我想将 mysite.com/news 重定向到 mysite.com/blog,但该代码将更多页面重定向到博客。

谁能帮我解释错误并告诉我如何正确重定向?

谢谢

【问题讨论】:

    标签: redirect nginx url-rewriting http-status-code-301 ngx-http-rewrite-module


    【解决方案1】:

    最佳做法是仍然使用location。如果您不希望 /news 以下的任何内容重定向到 /blog(例如,不需要通配符),那么以下就是您想要的,并且可能是创建单个别名的最有效方法:

    location = /news {
        return 301 /blog;
    }
    

    否则,如果您这样做,实际上需要一个通配符:

    location /news {
        rewrite ^/news(.*)  /blog$1 permanent;
    }
    

    附:另请注意redirect would cause 302 redirects; if you want 301, then the keyword is called permanent

    【讨论】:

      【解决方案2】:

      您不需要将它放在位置块内。只需一条重写规则就足够了。

      rewrite ^/news/?$ /blog redirect;

      【讨论】:

        猜你喜欢
        • 2012-06-17
        • 2012-02-27
        • 2020-10-29
        • 1970-01-01
        • 2018-08-14
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        相关资源
        最近更新 更多