2016-05-03 10 views
1

所以,如果我有這樣的當前設置。重寫除一些選擇路徑之外的所有子目錄

domain.com/some-path/ 
domain.com/some-path/locations/ 
domain.com/some-path/locations/some-location 
domain.com/some-path/locations/locb 
domain.com/some-path/locations/infinite-location-list 

這些是我不想重寫的URL模式。 some-path子目錄,它的子目錄locations子目錄,以及任何在locations之下的目錄。

some-path目錄下的所有其他子URL應該301指向/some-path/目錄。

我想是這樣的:

location ~* ^/some-path/(.+)? { 

    if ($request_uri !~ "^/some-path/locations/(.*)$") { 
    return 301 http://domain.com/some-path/; 
    } 

} 

/some-path/卡在重定向循環,和/some-path/locations/拋出一個服務器端的nginx如何實現我的目標404

的思考?

回答

1

這是一個壞主意,設計明智。如果用戶錯誤輸入domain.com/some-path/locations/some-location(根據您的說法)(這是無效的),那麼他們最終會將他們的所有輸入消除,並且他們必須從頭開始(或者可能只需去你的競爭對手)。

但是,如果你這麼堅持使用正則表達式:

if ($request_uri !~ "^/some-path/($|locations/.*$)") { 
     return 301 http://domain.com/some-path/; 
    } 

你可能必須在現有location內有這樣的,甚至保持在頂級水平,這取決於其餘您config,因爲只有一個location可以處理給定的請求。

+1

完全同意你的設計方面。我實際上正在推動這一要求。該網站的該部分/子目錄需要大量的重寫/重定向,這些重寫或重定向對於單獨編寫而言是完全無效的。如果我失敗了,這將成爲我的備用解決方案。 – Nathan

+0

@Nathan,好吧,瘋狂重寫沒什麼問題 - 結帳http://mdoc.su/,它也是我的github上的開源代碼,只要用戶體驗上升,而不是下降。 :-) – cnst

相關問題