2012-11-20 239 views
1

我想服務於不同的網址使用mod_rewrite,但無論我嘗試它只是不工作。URL重寫.htaccess

一個例子網址是 http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles

,我試圖用的.htaccess來更改URL:

http://www.site.com/country/tours/dynamic-part/new-york,los-angeles

RewriteEngine on 
RewriteBase/

RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$) 
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301] 

RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ /index.php/$1 [L] 

RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

任何想法?不過,我覺得我很接近但現在不是了:/

回答

2

的重寫規則不匹配的查詢字符串,請參閱 http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched

所以規則的.+city部分永遠不會匹配。

這應該壽工作...

RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$) 
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301] 

的subsitution可以讀回referenecs到的RewriteCond模式。

+0

我把它放在我的.htaccess(它不會像我的修改那樣返回500個內部服務器錯誤),但它不會刪除城市參數。我正在使用http://htaccess.madewithlove.be/來測試我的htaccess。 – GeorgeKaf

+0

檢查你的錯誤日誌,找到它不喜歡的文件。你怎麼知道該網站是可靠的。另一個有趣的點是你的'動態部分'示例包含一個連字符,但這不在你的正則表達式中。你可能也想要qsdiscard。 – barryhunter

+0

我會進一步探討它謝謝你。 – GeorgeKaf