1

If I want to block access to example.com/path/?xxxx (xxxx is variable), but allow access to example.com/path/, how should I write it in my .htaccess?

2
  • Are these urls valid ? Are there files there ? Oct 19, 2022 at 4:02
  • Ex: domain.com/someone/someone.php real and valid url. Attackers use domain.com/someone/someone.php?ddfdfd to bring down my site. If I write that parameter after a valid url, it does not cause a 404 error, it loads the url domain.com/someone/someone.php
    – Sanata
    Oct 23, 2022 at 4:33

1 Answer 1

0

You can block access (eg. trigger a 404 Not Found) when a specific path (/path/) is requested with any query string using mod_rewrite at the top of the root .htaccess file.

For example:

RewriteEngine On

RewriteCond %{QUERY_STRING} .
RewriteRule ^path/$ - [R=404]

The above matches the URL /path/ exactly. (Note there is no slash prefix on the URL-path matched by the RewriteRule pattern - the first argument.)

To block all URLs that contain any query string then change the RewriteRule pattern to just ^ (simply a start of string assertion, so it's successful for any URL-path). For example:

:
RewriteRule ^ - [R=404]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.