[英]Apache mod_rewrite [R,C] not working
我有以下设置
RewriteCond %{REQUEST_URI} ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302]
通过此设置,一切正常
example.com/search.php?what=foo&where=bar
被重写为
example.com/foo/bar
但是我还需要再次点击search.php,所以我添加了
RewriteCond %{REQUEST_URI} ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302,C]
RewriteRule ^ /search.php?what=$1&where=$2 [L]
现在,它“跳过”了重定向并对其进行了重写
从example.com/search.php?what=foo&where=bar到example.com/search.php?what=foo&where=bar
重定向不与C标志一起使用吗?
你不可以做这个:
RewriteCond %{REQUEST_URI} ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302,C]
RewriteRule ^ /search.php?what=$1&where=$2 [L]
对于http://www.example.com/search.php?what=foo&where=bar ,您无法同时击中/ foo / bar和/search.php?what=foo&where=bar,仅以唯一的方式重写结果网址。
在上述规则中, http: //www.example.com/search.php?what=foo&where=bar,它将匹配所有4行,最后一行给出/search.php?what=&where=,因为$ 1和$ 2为空。
因此解决方案是删除最后一行,如下所示:
RewriteCond %{REQUEST_URI} ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.