Regex mod_重写匹配查询_字符串

Regex mod_重写匹配查询_字符串,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,因此,我试图编写一个规则,如果将某些字符串传递给任何php脚本,该规则将以404响应。以下是我的想法: RewriteBase / RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC] RewriteRule ^$ [R=404,L] 该规则似乎只匹配www.domain.com/?string1或www.domain.com/?String2,但不匹配www.domain.com/whatever.php?var=string1或

因此,我试图编写一个规则,如果将某些字符串传递给任何php脚本,该规则将以404响应。以下是我的想法:

RewriteBase /

RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^$ [R=404,L]
该规则似乎只匹配www.domain.com/?string1或www.domain.com/?String2,但不匹配www.domain.com/whatever.php?var=string1或www.domain.com/directory/script.php?var=string1或www.domain.com/directory/1/script.php?var=string1等

有人能帮我指出我做错了什么吗

最好的


-Iulian

您的重写规则需要一个空路径。试着这样做:

RewriteBase /

RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^.*$ - [NC,L]

正如Kevin所说,在查询字符串之前需要一个空URL,带有
^$
。您不需要所有的
*
,也不必匹配完整的字符串。这将起作用,您也不需要重写库:

RewriteCond %{QUERY_STRING} (?:string1|string2) [NC]
RewriteRule ^ - [R=404,L]

?:
只是说不要捕获这个,它只是用于分组。
^
是一种匹配任何内容的方法。
-
表示不要更改URL。

我还想补充一点,我已经尝试了^(.*)(string1 | string2)(.*)和(string1 | string2)$,行为与上面相同。