Search code examples
apache.htaccessmod-rewrite

htaccess block requests by querystring


There is a way to block all the requests with a certain querystring?

I should block all the request that have "?userid=1234" or "&userid=1234"

For example:

/directory/page.php?userid=1234&var2=abc&var3=..
/directory/page.php?var1=test&userid=1234&var2=abc&var3=..

The directory and the page are always the same.

I know it's possibile, but i'm not sure how..


Solution

  • You can check QUERY_STRING and test if it contains userid=1234.
    If so, then forbid it

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} \buserid=1234\b [NC]
    RewriteRule ^ - [F]
    

    Note: \b is a word boundary anchor. Having it before and after the pattern we want to match makes sure that the rule will match exactly on userid=1234 and not on, e.g., xxxuserid=1234 or userid=1234xxx.