I need a rewrite condition for returning e.g. 403, when a query contains characters, which are not in my "whitelisted" character class. The query beginns with q= So my attempt was:
RewriteCond %{REQUEST_URI} ^/generic/
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} q=[^a-z]+
RewriteRule .* - [F]
It is working almost. Only problem is, when the query beginns with e.g. "a", any following blacklisted characters don't trigger the rule
Example:
q=abc -> rule does not match -> fine
q=Abc -> rule matches -> fine
q=abC -> rule does not match -> not fine
So when the forbidden characters are not the first character after "q=", the rule is not working.
What am I missing? Can someone help me?
BR
With your shown attempts, please try following htaccess rules file. 2 important points here, 1st you can use ^ in spite of .* in last forbidding Rule. 2nd is you need to use NC to cover lower case to match small and capital letters in regex.
RewriteCond %{REQUEST_URI} ^/generic/ [NC]
RewriteCond %{REQUEST_METHOD} ^GET$ [NC]
RewriteCond %{QUERY_STRING} q=[^a-z]+ [NC]
RewriteRule ^ - [F]
OR use following rules to match exact query string use ^ and $ anchors to match exact query string value:
RewriteCond %{REQUEST_URI} ^/generic/ [NC]
RewriteCond %{REQUEST_METHOD} ^GET$ [NC]
RewriteCond %{QUERY_STRING} ^q=[^a-z]+$ [NC]
RewriteRule ^ - [F]