代码之家  ›  专栏  ›  技术社区  ›  Alex

某些查询参数/值对的重写秒匹配

  •  2
  • Alex  · 技术社区  · 17 年前

    如果某个参数/值对在querystring中,我必须重定向到另一个主机。

    到目前为止,我有

    RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23&?
    RewriteRule ^(.*)$ http://anotherserver.com/$1 [R,NC,L]
    

    适用于:

    /index.php?id=95&abc=23
    /index.php?abc=23&id=95
    /index.php?id=95&abc=23&bla=123
    

    但它也符合 /index.php?id=95&abc=234 例如。

    我需要一个完全匹配的图案 abc=23 无论发生在哪里。

    对此有什么建议吗?-)

    2 回复  |  直到 13 年前
        1
  •  6
  •   LukáÅ¡ Lalinský    17 年前

    我想尝尝这个雷杰克斯 (&|^)abc=23(&|$) 比赛只对 %{QUERY_STRING} .

        2
  •  1
  •   Andrew    17 年前

    The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches colour or color.

    RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23&?
    

    您正在将abc=23&或abc=23与字符串的其余部分进行无约束匹配,因此abc=234是有效的匹配。您真正想要的是&或其他任何东西。我不确定此regexp在Apache中是否合法,但它将写为:

    RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23(&|$)
    

    这是我在 favourite online RegExp tester :

    abc=23&def=123
    abc=234
    abc=23