Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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 如何重定向重写的查询字符串URL_.htaccess_Redirect_Url Rewriting - Fatal编程技术网

.htaccess 如何重定向重写的查询字符串URL

.htaccess 如何重定向重写的查询字符串URL,.htaccess,redirect,url-rewriting,.htaccess,Redirect,Url Rewriting,我使用以下规则 RewriteRule ^([a-z0-9\-]+)/?$ category.php?cat=$1 [NC,L] RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ category.php?cat=$1&subcat=$2 [NC,L] 重写以下URL mynews/category.php?cat=News mynews/category.php?cat=News&subcat=9 到 它只是被改写了。但如何将这些查询字

我使用以下规则

RewriteRule ^([a-z0-9\-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ category.php?cat=$1&subcat=$2 [NC,L]
重写以下URL

mynews/category.php?cat=News
mynews/category.php?cat=News&subcat=9

它只是被改写了。但如何将这些查询字符串URL自动重定向到重写的URL

我按照答案中的建议使用了以下规则,但不起作用

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^&]+)&subcat=([0-9]+)
RewriteRule ^category\.php$ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^\ &]+)
RewriteRule ^category\.php$ /%1/? [L,R]

RewriteRule ^([a-z0-9\-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ category.php?cat=$1&subcat=$2 [NC,L]

您需要添加一些规则来执行外部重定向。这些重定向规则需要放在您已有的重写规则之前。所以它们看起来像这样:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^&]+)&subcat=([0-9]+)
RewriteRule ^category\.php$ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^\ &]+)
RewriteRule ^category\.php$ /%1/? [L,R]
这些将看到
/category?cat=
请求,并从外部将浏览器重定向到外观更好的URL。我们需要匹配
%{The_REQUEST}
的原因是,我们要确保您已经拥有的重写规则不会干扰重定向规则,因为重写引擎将循环遍历这些规则

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^&]+)&subcat=([0-9]+)
RewriteRule ^category\.php$ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^\ &]+)
RewriteRule ^category\.php$ /%1/? [L,R]