的Nginx 301重定向语法错误

问题描述:

我才发现,我的nginx的语法是不正确的:的Nginx 301重定向语法错误

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

我想重定向到mysite.com/news但mysite.com/blog代码被重定向页,以博客。

任何人都可以帮助我解释错误并告诉我如何正确重定向?

谢谢

最好的做法是仍然使用location。如果你不希望下面/news什么重定向到/blog(例如,无需通配符),再下面是你想要的,可能是建立一个单一的别名,最有效的方法:

location = /news { 
    return 301 /blog; 
} 

否则,如果您确实需要通配符:

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

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

你不需要把它放在位置块内。只需要一个重写规则就足够了。

rewrite ^/news/?$ /blog redirect;