.htaccess 301使用mod_重写重定向
我正在.htaccess下使用mod_rewrite,并尝试重定向(R=301)一个URL,如下所示:.htaccess 301使用mod_重写重定向,.htaccess,mod-rewrite,url-rewriting,s3-rewrite-rules,.htaccess,Mod Rewrite,Url Rewriting,S3 Rewrite Rules,我正在.htaccess下使用mod_rewrite,并尝试重定向(R=301)一个URL,如下所示: http://domain/index.php?folder=AB_CD 指向这样的URL http://domain/AB/CD/ 如何编写规则?请在root/.htaccess中尝试以下代码: RewriteEngine On RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC] RewriteRule ^in
http://domain/index.php?folder=AB_CD
指向这样的URL
http://domain/AB/CD/
如何编写规则?请在root/.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\u bar),根据regex模式,如果url具有有效的查询字符串,则处理规则
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
php?如果满足条件,查询字符串将被重定向到/query/strings
重写目标末尾的空问号?很重要,因为它会丢弃原始查询字符串,而不使用/index.php?folder=foo\u bar重定向到/foo/bar/?folder=foo\u bar追加旧查询字符串
(希望这有帮助!)