Block certain requests starting with specific string using .htaccess in apache

.htaccessapache-2.2

54.215.234.39 - - [02/Apr/2014:10:51:35 -0400] "GET /ProductSearch.aspx?qs=94307 HTTP/1.1" 404 11414 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0"

I am getting a lot of these requests sent to my apache server from various ip addresses. We used to use a aspx based site. Now it's php. These requests are flooding the server. How can I block all requests that have the string "ProductSearch.aspx" in them?

Best Answer

You could send a 403 Forbidden (access denied) in case /ProductSearch.aspx is requested :

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ProductSearch.aspx
RewriteRule ^(.*)$ - [F,L]

However, note that these requests will still reach your server and will appears in your access.log (but with a 403 instead of a 404).

To avoid this you will have to filter before your web server. This could be done with a Reverse Proxy but this is a full other point.


Also, as mentionned by @Michael Hampton in comments, you could redirect all requests that ask for an *.aspx file to an *.php page.

(assuming, e.g, that yourpage.aspx has been replaced by yourpage.php) :

RewriteEngine On
RewriteRule  ^(.*).aspx$ $1.php [R=301]

This will redirect

  • http://mydomain.com/ProductSearch.aspx?qs=94307

to

  • http://mydomain.com/ProductSearch.php?qs=94307
Related Topic