2013-02-12 106 views
3

我正在使用nginx與我的Java應用程序,我的問題是nginx正在合併斜槓,我無法將我的網站重定向到正確的版本。Nginx merge_slashes重定向

例如:

http://goout.cz/cs/koncerty///praha/ 

合併到

http://goout.cz/cs/koncerty/praha/ 

,然後我無法認識到錯誤的URL並執行重定向。

我試圖設置

merge_slashes off; 

然後:

rewrite (.*)//(.*) $1/$2 permanent; 

但這並沒有影響和//停留在URL。

我該如何做到這一點?

回答

5

試試這個(未經測試):

merge_slashes off; 
rewrite (.*)//+(.*) $1/$2 permanent; 

這可能導致多次重定向如果有斜線的多組雖然。

像這樣:

http://goout.cz/////cs/koncerty///praha/ 

可能會去:

http://goout.cz/cs/koncerty///praha/ 

後來總算:

http://goout.cz/cs/koncerty/praha/ 
1

這工作不錯,但我的設置添加port_in_redirect off;是必要的。

1

我們遇到了同樣的問題,因爲有錯誤,我們在URL中添加了兩個斜槓,而nginx將爲兩個斜槓返回301錯誤代碼。

對我來說,解決辦法是:

添加merge_slashes off;nginx.conf文件,和在該位置的部分,添加rewrite (.*)//+(.*) $1/$2 break;

我的位置設置如下圖所示:

location/{ 
     rewrite (.*)//+(.*) $1/$2 break; 
     proxy_pass http://http_urltest; 
     proxy_http_version 1.1; 
     proxy_set_header Connection ""; 
     proxy_buffers 4096 32k; 
     proxy_buffer_size 32K; 
     proxy_busy_buffers_size 32k; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 

後添加這兩行代碼,當我用兩個斜槓訪問我的URL時,它會用一個斜槓返回結果。

-1

試試這個(包括nginx的僅與openresty配置nginx的),你可以通過做這些301重定向提高網站的搜索引擎優化

請保持服務器節內此代碼爲您的nginx網站的conf文件

server { 

........ 
........ 

set $test_uri $scheme://$host$request_uri; 
if ($test_uri != $scheme://$host$uri$is_args$args) { 
    rewrite^$scheme://$host$uri$is_args$args? permanent; 
} 

location { 
    ................ 
    ................ 
} 

}

其工作適合我和我使用此代碼現在

例如: -

請求URL-http://www.test.com//test///category/item//value/

結果網址: - http://www.test.com/test/category/item/value/

301重定向使網站的搜索引擎優化將不會下降

+0

'如果' 是邪惡:HTTPS://www.nginx。 COM /資源/維基/開始/主題/深度/ ifisevil / – MacroMan 2018-02-14 11:16:35