How to block an IP in your .htaccess file

Sometimes you have to stop an IP in it’s tracks. Perhaps they are attempting to exploit your site. Or they are attempting to brute force a login. Maybe they are just downloading too much of your site and using up too many resources.

Whatever the reason, you can block them very easily. Simply fire up an editor and place the following in your .htaccess file:

order allow,deny
deny from
deny from
allow from all

You can have dozens of entries in your .htaccess file. Note that the second “deny from” line IP address is only 3 octets instead of 4. This will block any IP address that matches the first 3 octets – useful for banning multiple IP’s in the same range.