How to filter all Apache URL containing string but

2019-08-03 07:08发布

I'm trying to filter all clients (using .htaccess) who try to access addresses containing for instance path=\ in the URL, but not those URL's that contain _wpnonce=1234567890.

The logic is: block all but those who also have the _wpnonce parameter.

The blocking, without testing if it contains the substring looks like this:

RewriteCond %{QUERY_STRING} (menu|mod|path|tag)\=\.?/? [NC,OR]

Is there a way to also include my substring negation there?

1条回答
Deceive 欺骗
2楼-- · 2019-08-03 07:31

The documentation says that the pattern is a Perl-compatible regular expression, so you should be able to use a negative lookahead assertion:

RewriteCond %{QUERY_STRING} ^(?!.*_wpnonce=1234567890).*(menu|mod|path|tag)\=\.?/? [NC,OR]

though it might be clearer to put it in a separate RewriteCond, provided you can get all the [OR]s to work out. If it weren't for the [OR]s, you could just write:

RewriteCond %{QUERY_STRING} !_wpnonce=1234567890 [NC]
RewriteCond %{QUERY_STRING} (menu|mod|path|tag)\=\.?/? [NC]
查看更多
登录 后发表回答