2016-11-29 50 views
1
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^test\.hemsida\.eu$ [OR] 
RewriteCond %{HTTP_HOST} ^www\.test\.hemsida\.eu$ 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ 
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ 
RewriteRule ^(.*)$ "https\:\/\/testar\.se\/sv/$1" [R=301,L] 

所以這一塊都將流量重定向從test.hemsida.eu到https://testar.se/sv/$1的Apache 301重定向添加語言前綴,如果不存在

但問題是,如果test.hemsida.eu包含路徑SV,它重定向:https://testar.se/sv/sv/test

如何擺脫一個sv,但仍然保持它,如果它不在URL中?

+0

從您的重寫規則的目的地中刪除** sv **。 – starkeen

+0

如果我這樣做,它不會將test.hemsida.eu重定向到testar.se/sv –

+0

對不起,我誤解了你的問題。你可以使用這種模式** ^(?: sv)?(。*)$ **,所以它匹配sv作爲可選路徑,但不會將它追加到目標。 – starkeen

回答

0

您可以使用它像這樣通過調整你的正則表達式:在模式的開始

RewriteEngine on 

RewriteCond %{HTTP_HOST} ^(?:www\.)?test\.hemsida\.eu$ [NC] 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ [NC] 
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ [NC] 
RewriteRule ^(?:sv/)?(.*)$ https://testar.se/sv/$1 [NC,NE,R=301,L] 

(?:sv/)?(可選的比賽)將確保你隨時捕捉/sv/後,無論發生什麼事,或者有沒有/sv/然後完成比賽。