Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 301使用mod_重写重定向_.htaccess_Mod Rewrite_Url Rewriting_S3 Rewrite Rules - Fatal编程技术网

.htaccess 301使用mod_重写重定向

.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

我正在.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 ^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追加旧查询字符串

(希望这有帮助!)