【问题标题】:Question mark in the end of RewriteRuleRewriteRule 末尾的问号
【发布时间】:2009-10-03 09:22:49
【问题描述】:
RewriteCond %{QUERY_STRING}  ^id=(.*)$
RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1 [R=301,L]
and
RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1? [R=301,L]

第一种情况的结果是
new-site.com/newpage-3?id=3

new-site.com/newpage-3

第二个重写规则中的问号是什么意思?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    那个?在目的地的末尾(目的地不是正则表达式)意味着去那个目的地没有查询字符串。

    RewriteCond %{QUERY_STRING}  ^id=(.*)$
    RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1 [R=301,L]
    

    如果查询字符串只包含一个 id,它会存储在目的地中使用的值,所以如果你有

    http://foo.com/oldpage.php?id=54
    

    你最终会得到

    http://new-site.com/newpage-54?id=54
    

    如果你有

    RewriteCond %{QUERY_STRING}  ^id=(.*)$
    RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1? [R=301,L]
    

    你会去同一个目的地,但查询字符串是空的,所以去

    http://foo.com/oldpage.php?id=54
    

    最终会在

    http://new-site.com/newpage-54
    

    【讨论】:

    • "如果查询字符串只包含一个id",如果没有,是否只对id有效?
    • RewriteCond %{QUERY_STRING} ^id=(.*)$ 表示该规则仅在查询字符串匹配时适用。在这种情况下,它将匹配任何以 id= 开头的查询字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多