
答案1
location = / {
rewrite ^ http://site.com/blog/ redirect;
}
這只會專門針對根執行請求。如果您需要捕獲所有內容(重定向http://site.com/somearticle/something.html
到http://site.com/blog/somearticle/something.html
),那麼您將需要更多的內容:
location /blog/ {
# Empty; this is just here to avoid redirecting for this location,
# though you might already have some config in a block like this.
}
location / {
rewrite ^/(.*)$ http://site.com/blog/$1 redirect;
}
答案2
試試這個:
location = / {
return 301 /blog/;
}
這裡的關鍵是'='象徵。
答案3
這對我不起作用。這是有效的:
開啟您網站的 NGINX 設定檔。在伺服器區塊內部,新增根目錄的路徑並設定檔案的優先順序:
root /mnt/www/www.domainname.com; index index.php index.html index.htm;
建立一個空的位置區塊前您所有其他位置區塊:
location /latest { # Nothing in here; this is to avoid redirecting for this location }
註解掉 location / {} 區塊中的根目錄指令並新增重定向,使其看起來像這樣:
location / { # root /mnt/www/www.domainname.com; index index.php index.html index.htm; rewrite ^/(.*)$ http://www.domainname.com/latest/$1 redirect; }
確保您的位置 ~ .php$ 區塊將其根指向
root /mnt/www/www.domainname.com;
這為我解決了這個問題。
答案4
對於只是主頁,沒有其他內容,我會使用:
location / {
rewrite ^/$ /blog/ redirect;
}
其他任何內容,例如 /foo/ 都不會被重定向到 /blog/ 。