简体   繁体   English

Apache mod_rewrite [R,C]不起作用

[英]Apache mod_rewrite [R,C] not working

I have following setup 我有以下设置

RewriteCond %{REQUEST_URI}  ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302]

With this setup everything is working as excepted 通过此设置,一切正常

example.com/search.php?what=foo&where=bar example.com/search.php?what=foo&where=bar

is rewritten to 被重写为

example.com/foo/bar example.com/foo/bar

But I also need to hit search.php again so I added 但是我还需要再次点击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]

Now it 'skips' the redirect and its rewritten 现在,它“跳过”了重定向并对其进行了重写

from example.com/search.php?what=foo&where=bar to example.com/search.php?what=foo&where=bar 从example.com/search.php?what=foo&where=bar到example.com/search.php?what=foo&where=bar

Is the redirect not working with the C flag? 重定向不与C标志一起使用吗?

You cannot do this: 你不可以做这个:

RewriteCond %{REQUEST_URI}  ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302,C]
RewriteRule ^ /search.php?what=$1&where=$2 [L]

For http://www.example.com/search.php?what=foo&where=bar , you cannot hit at the same time /foo/bar and /search.php?what=foo&where=bar, Rewrite results only in a unique URL. 对于http://www.example.com/search.php?what=foo&where=bar ,您无法同时击中/ foo / bar和/search.php?what=foo&where=bar,仅以唯一的方式重写结果网址。

In the above rules, http://www.example.com/search.php?what=foo&where=bar , it will match all the 4 lines, at the last line, it gives /search.php?what=&where=, as $1 and $2 are empty. 在上述规则中, http: //www.example.com/search.php?what=foo&where=bar,它将匹配所有4行,最后一行给出/search.php?what=&where=,因为$ 1和$ 2为空。

So the solution is to remove the last line, like this: 因此解决方案是删除最后一行,如下所示:

RewriteCond %{REQUEST_URI}  ^/search.php$
RewriteCond %{QUERY_STRING} ^what=(.*)&where=(.*)$
RewriteRule ^ /%1/%2? [R=302]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM