Banning an IP with .htaccess file

Sometimes there are situations where you would like to ban an IP to stop the misuse of your site and its services. Ensuring the security of your website is crucial in today’s digital landscape. One effective way to enhance security is by employing IP address blocking through the use of the .htaccess file. The .htaccess file is a powerful configuration file that can be utilized to control various aspects of your web server, including restricting access based on IP addresses.

So here is a technique for banning an IP with the help of .htaccess file tp provide an added layer of protection to your website. You will have to create an empty .htaccess file or use an existing one on server if it is already over there. The .htaccess file is typically found in the “root directory” of your website. You can use FTP (File Transfer Protocol) or your web hosting control panel to access and edit this file. Before making any changes, it’s recommended to create a backup of the .htaccess file to avoid any unintended issues.

The code to ban an IP with .htaccess file.

order allow,deny
deny from ###.###.###.###
allow from all

You can replace the # with the IP which needs to be banned.

To ban multiple IP’s use the following code

order allow,deny
deny from ###.###.###.###
deny from ###.###.###.###
deny from ###.###.###.###
deny from ###.###.###.###
allow from all

Where the # can be replaced by the IP which needs to be banned from your server.

If you want to ban a range of IP Addresses then use the following code

order allow,deny
deny from ###.###.###.*
allow from all

Here the asterisk (*) is suggesting a wildcard match for whole range. But use this carefully.

If in some cases you only want to allow access from specific IP address or addresses then you can use the following code

order allow,deny
deny from all
allow from ###.###.###.*

If you have cPanel on your hosting server then IP Deny Manager would be easier to use.

The above code/guide is free to use but author of this site is not responsible for anything that might go wrong. Use it at your own risk.

You Might Also Like