How to block IPs via an .htaccess file

You can quickly block IP addresses from accessing your sites or specific folders using a .htaccess file. Note that the ‘.’ in front of htaccess makes this a hidden file. Please make sure your have the  ‘View Hidden Files’ option checked in your FTP program or through File Manager.

If you do not already have a .htaccess file, you can create or upload a new file and name it ‘.htaccess’. To block access to your entire site, add the .htaccess file under the public_html folder. To block access to a specific folder, add the .htaccess file within that folder.  For example, to block access to http://domain.com/dev, add the .htaccess file under the public_html/dev folder.

To block IPs, add the following lines in the .htaccess file:

order allow,deny
deny from 255.0.0.0
deny from 123.45.6.7
allow from all

This will block IPs 255.0.0.0 and 123.45.6.7 from access your files. To add more blocks, simply add more “deny from IP_ADDRESS” lines above the allow from all line.

You may find that you need to block access to a folder for everyone except you, or your developers. To block access for everyone except for specific IPs, add the following to your .htaccess file:

order allow,deny
allow from 255.0.0.0
allow from 1.1.1.1
deny from all

Be the first to comment

Leave a Reply

Your email address will not be published.


*