0

我才發現,我的nginx的語法是不正確的:的Nginx 301重定向語法錯誤

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

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

任何人都可以幫助我解釋錯誤並告訴我如何正確重定向?

謝謝

回答

1

最好的做法是仍然使用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

0

你不需要把它放在位置塊內。只需要一個重寫規則就足夠了。

rewrite ^/news/?$ /blog redirect;