Use apache to block a url with specific query string

11,427

You can use ModRewite to check for query strings and redirect or block a page. Using your example:

RewriteCond %{QUERY_STRING} var=var
RewriteCond %{QUERY_STRING} date=12/12/12
RewriteCond %{QUERY_STRING} var2=word\+word
RewriteRule .* - [F]

(There's an implicit AND between the RewriteCond statements)

This would block ([F]) all pages (.*) that had all three of those querystring parameters and values.

UPDATED to use more specific examples by the OP

Share:
11,427

Related videos on Youtube

rix
Author by

rix

Updated on September 18, 2022

Comments

  • rix
    rix almost 2 years

    I have this url:

    mysite.com?var=var&var2=var2&var3=var3
    

    I would like to block this specific url but not in any way affect other query strings on the same base url.

    Is it possible to do this?

    Thanks,

  • rix
    rix over 12 years
    Actually, that doesn't appear to work - any other ideas?
  • Scott Coldwell
    Scott Coldwell over 12 years
    It worked in my local Apache install. Is RewriteEngine On? What happens when you hit the URL above?
  • ravi yarlagadda
    ravi yarlagadda over 12 years
    @rix Keep in mind that there may be characters in your string that need to be escaped with a ` when used in a regex. If this doesn't work, try setting a RewriteLog` and turn RewriteLogLevel to 9, and let us know what that gives you.
  • rix
    rix over 12 years
    Ah right, I have / in the var ie a date=12/12/12 and other than that just + for spaces. ie var=word+word - does the + and the / need to be escaped?
  • Scott Coldwell
    Scott Coldwell over 12 years
    The + needs to be escaped: RewriteCond %{QUERY_STRING} var=word\+word