Sunday 20 May 2012

Block a site visitor by IP

If you have a web site, you know that fight against bad spammers or a undesirable visitor (aggressive, insulting...) can be useful. ;-) The method to block a particular IP address is a bit the same that to get rid of sites which steal your images, with a .htaccess file. This is even more basic because this is a base function of .htaccess which does not need the Rewrite module, or any other.

~

Here is the code to put in your .htaccess file to block the IPs from 1.2.3.4 to 1.2.3.6:

Order Allow,Deny
Allow from all
Deny from 1.2.3.4
Deny from 1.2.3.5
Deny from 1.2.3.6

~

You can also block a visitor by his host name, by writing:

Deny from domain.com

but it requires that your server resolves domain name from IP addresses, that is generally deactivated because it consumes resources for a little utility, so this have less chance to work. If it works, that can be useful to ban spam servers which pollute your comments but with different IPs. ;-)

Hopefully if your server does not resolve addresses, you can get around this limitation by using IP ranges:

Deny from 1.2.5
Deny from 1.2.3.7 1.2.3.255

will block all IPs beginning by 1.2.5 and all the range between 1.2.3.7 and 1.2.3.255.

~

This is detailed in the Allow documentation of the Apache server. The different values are of course the same for the Deny command instead of Allow.


café Did this article help you? 
Buy me a coffee!

Leave a comment (all comments are moderated, don't waste time with spam)

Azure Dev