Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 重写特定查询参数/值对的条件匹配_Regex_.htaccess_Mod Rewrite - Fatal编程技术网

Regex 重写特定查询参数/值对的条件匹配

Regex 重写特定查询参数/值对的条件匹配,regex,.htaccess,mod-rewrite,Regex,.htaccess,Mod Rewrite,如果查询字符串中有某个参数/值对,我必须重定向到另一个主机 到目前为止我有 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&

如果查询字符串中有某个参数/值对,我必须重定向到另一个主机

到目前为止我有

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的模式,不管它发生在哪里

有什么建议吗?:-)

我会尝试这个正则表达式
(&|^)abc=23(&|$)
,匹配只针对
%{QUERY_STRING}

您正在将abc=23&或abc=23与未受约束的字符串的其余部分匹配,因此abc=234是有效的匹配项。你真正想要的是&或者没有别的。我不确定这个RegExp在Apache中是否合法,但它将被写成:

RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23(&|$)
以下是我在工作中使用的测试用例:


谢谢,我知道问号使前面的标记成为可选标记。我确实添加了它,因为否则它就不会匹配index.php?id=95&abc=23(只有在23后面有一个“&”)哦,我看到卢卡斯已经写过了。嗯,需要打字更快。顺便说一下,在字符类中不需要转义
<代码>[&?]完全有效,并匹配一个符号和或问号。
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23(&|$)
abc=23&def=123
abc=234
abc=23