2016-01-22 99 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

谢谢你的回答 –