【问题标题】:Apache mod_rewrite [R,C] not workingApache mod_rewrite [R,C] 不工作
【发布时间】:2013-11-24 19:56:48
【问题描述】:

我有以下设置

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 标志一起使用吗?

【问题讨论】:

    标签: regex apache mod-rewrite rewrite


    【解决方案1】:

    你不能这样做:

    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,重写结果只在一个唯一的 URL 中。

    在上面的规则中,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]
    

    【讨论】:

    • 抱歉,这不是一个选项。我需要将 example.com/search.php?what=foo&where=bar 重写为 example.com/foo/bar 但我希望将 example.com/foo/bar 重写为 example.com/search.php?what=foo&where =酒吧
    猜你喜欢
    • 2012-12-19
    • 2015-09-22
    • 2018-09-14
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2011-04-14
    相关资源
    最近更新 更多