How to Block IP Addresses on Linux Using .htaccess

Sometimes you may want to block certain people from visiting your site. They may be trying to guess your password or maybe you simply do not want them to have access to your website. On a Linux plan, this can be handled easily via .htaccess.

In your public_html file should be a file called .htaccess (if there isn’t one, you can simply create an empty file named .htaccess

Open this file in a text editor and at the top of the file insert the following:

order allow,deny
deny from
allow from all

Replace with the IP address you would like to block. This will allow all IPs to access your site except the one you list.

If you would like to block multiple IPs, you can insert the following instead:

order allow, deny
deny from
deny from
deny from
allow from all

If you would like to block an entire range of IPs, you can do that as well. In this case, let’s say you want to block the entire 999.0.0 range. (Do note that this is not a valid IP and I am just using this IP for an example.) This would include IPs from 999.0.0.0 to 999.0.0.255. Instead of listing all 256 IPs separately, you can block the entire range like this: