[英]how to redirect rewritten query-string URL
我正在使用以下规则
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
至
mynews/News/
mynews/News/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=<something>
请求,并在外部将浏览器重定向到外观更好的URL。 我们需要与%{THE_REQUEST}
进行匹配的原因是,由于重写引擎将遍历规则,因此我们确保您已经具有的重写规则不会干扰重定向规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.