简体   繁体   中英

mod_rewrite match QUERY_STRING

So I'm trying to write a rule that will respond with a 404 if certain strings are passed to any of the php scripts. Here's with what I came up with:

RewriteBase /

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

That rule appears to be matching only www.domain.com/?string1 or www.domain.com/?String2, but not www.domain.com/whatever.php?var=string1 or www.domain.com/directory/script.php?var=string1 or www.domain.com/directory/1/script.php?var=string1 and so on.

Can anyone help and point out what I am doing wrong?

Best,

-Iulian

Your RewriteRule is requiring an empty path. Try it like this:

RewriteBase /

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

As Kevin says, you are requiring an empty URL before the query string, with ^$ . You don't need all the .* , you don't have to match the full string. This will work, you don't need the RewriteBase either:

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

The ?: just says don't capture this, it's only for grouping. The ^ is a way of matching anything. The - says don't change the URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM