2016-01-22 93 views
1

我用下的.htaccess mod_rewrite的工作,而且我想重定向(R = 301),像這樣的URL:301重定向與mod_rewrite的

http://domain/index.php?folder=AB_CD 

到URL這樣

http://domain/AB/CD/ 

我該如何寫規則?

回答

1

嘗試下面的代碼在根/ htaccess的:

RewriteEngine On 


RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC] 
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R] 

闡釋:

RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC] 

檢查以確保該URL(的index.php)具有特定的鍵和值的查詢字符串, (folder = foo_bar)根據正則表達式模式,如果url具有有效的查詢字符串,則規則被處理

RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R] 

index.php?query_strings被重定向到/ query/strings,如果條件滿足。

空的問號?重寫目標末尾的非常重要,因爲它丟棄了原始查詢字符串,沒有它/index.php?folder=foo_bar重定向到/ foo/bar /?folder = foo_bar附加舊查詢字符串。

(希望這有助於您!)

+1

謝謝你的回答 –