Using .htaccess to block an IP address

You can use .htaccess file in order to prevent an access to your webpage from specific IP. A .htaccess is a kind of settings file that can be used for things like establishing password-protected directories and to reduce accessibility based on IP. What other things you can do with .htaccess file, read my posts here.

To ban an IP from obtaining your web page, make a .htaccess file within your root folder (usually public_html or www) and add the following settings in the file:

order allow,deny
deny from 32.4.32.33
allow from all

It’s easy to know what exactly is going on here. “order allow,deny” is particularly in this order so that the allow rules are checked first, and if someone receive a deny rule, their server request is rejected (i.e. they are blacklisted).

Within the example above, we’re blacklisting 127.0.0.1. In actual use, you will need to modify this to the IP address which you wish to blacklist.You can, of course, blacklist several IP details by adding several deny guidelines on a individual line. For example:

order allow,deny
deny from 93.84.29.121
deny from 89.32.42.422
deny from 83.43.2.34
deny from 12.34.2.44
allow from all

If you want to prevent an whole IP range, you can exclude a particular range in the .htaccess rule:

order allow,deny
deny from 42.53.1
allow from all

This will blacklist IP details in the range of 42.53.1.0 to 42.53.1.0.255

If  you are not easy to use .htaccess file you may use IP deny manager available into your cPanel to block access to your website.

Ip deny managerYou may start by adding the IP into IP Address or Domain box after that page can refresh with the notification that Ip address will not be able to access your site.

We have blocked a single IP, multiple IPs and a whole range so far. Whats about blocked a whole country? There are many different IPs blocks that are assigned to a country and it is difficult for any one to short that out by himself. The good thing here is there are several sites that can help us in obtaining a .htaccess optimized IP blocks of any country. ip2location is such a website, you can simply visit it, select the country and then copy paste the whole long output in your .htaccess file.

 

One Response

  1. Emmanuel
    Emmanuel at | | Reply

    The .htaccess sounds like a longer way to do it.. I would suggest the friendly user interface on CSF where you just paste the IP on the Quick Deny (csf.deny) box .. If you add the comment “DO NOT DELETE” it stays there even if you flush all blocks..

Leave a Reply