Ban an IP address: Brute force attacks are one of the most common forms of attacks made on WordPress websites today. In brute force attacks, hackers make repeated login attempts on the WordPress login page. One of the ways to prevent brute force attacks on your site is to ban IP addresses of the hackers.

Every computer, mobile, and tablets are hooked to an IP address. Even, the device on which you are reading this post has an IP address (try Googling “What is my IP?” to know yours). A hacker launching attack on your WordPress website has an IP address. You can record these IP addresses and blocking them from accessing your site altogether. The .htaccess file is one way of achieving it.

Before we get into how to ban an IP address using the .htaccess let’s have a look at some of the disadvantages:

1. You Site May Crash

The .htaccess is one of the most important files of your website. It’s a configuration file that you can modify but with caution. One mistake can prove to be catastrophic. People without any technical knowledge are usually recommended to avoid making a change to the .htaccess file. There are tons of tutorials available online on how to edit .htaccess but nevertheless, editing the file comes with a set of risk. Any misconfiguration can cause your site to misbehave or even crash.

2. You May Block Search Engine Crawlers

Sometimes a misconfiguration can lead to blocking search engine bots. If your websites are not crawled, they won’t be indexed, nor will they ever appear on the search engines. Unintentionally, you may case an SEO catastrophe and start losing search engine rankings of your site. 

3. You Can Accidentally Ban a Valid User

We have seen discussions on forums where site owners lamented of accidentally banning of IP address of an admin. The risk of accidentally blocking users of your site is always present. If the IP address is incorrect, you may end up blocking someone you didn’t intend to block. 

Sometimes, site owners unwittingly block out a number of visitors. In places like an office complex, there can be many individuals users using the same IP address. At times, an entire country (albeit a small one) could be using only a handful of IP addresses. in such cases, when you ban an IP address, you can accidentally end up blocking a large number of visitors.

4. You May Miss Out on Potential Audience

The central idea behind having a website online is to draw visitors, reach out to an audience who can be located anywhere in the world. But when some websites resort to country blocking, they risk blocking out potential audience. Regardless of their geographical location, the content of your site is valuable to all people. You may have a good reason to block an entire country but at the same time, you are also ignoring a good pool of audience.

5. Hackers can Still Access Your Site

Changing IP address may discourage a novice but a seasoned hacker will remain undeterred as he knows how to overcome such techniques. Hackers have access to network IP addresses and they very rarely stick to one IP because it can get caught and blacklisted. Hackers also rarely target a single site. They launch attacks on a number of sites simulteneously using a cluster of IP addresses they have built. Hence, when you ban an IP address, they switch to another. In such cases, banning IP is only a temporary relief.

These are the disadvantages of banning an IP address. If you still want to go ahead with the process, here’s what you need to do:

How to Ban an IP Address Using the Htaccess?

The first obvious step is to identify these addresses. Security plugins like MalCare Security Service show you the IPs of users who have been trying to log into your site unsuccessfully. Going through the log, it’s easy to notice how some of the IP addresses are failing to log in on a daily basis. Besides the IP address, you will also notice the usernames that were being used to log in. You can check if the username exists to make sure you are not tagging a valid user as bad traffic. And after you have a list of bad IP addresses you want to ban, open your .htaccess file.

ban an IP address

IP addresses of failed login attempts made on a site

The .htaccess is a configuration file that you can access from your file manager.

Step 1: To access the .htaccess, open your web host account and go cPanel. Select File Manager, and it’ll take you a page that looks somewhat like this:

ban an IP address

This is a typical file manager page

Step 2: On the left-hand side, there’s a public_html folder. In the public_html, you should find the .htaccess file.

ban an IP address

The .htaccess file can be found inside the ‘public_html’ folder

Step 3: Right click on the file and select Edit.

ban an IP address

Edit and copy paste a code to ban an IP address

Step 4: And then simply paste the following code in your .htaccess file:

order allow,deny 

deny from 192.168.20.10 

allow from all

The above code will successfully block the IP “192.168.20.10”. You can replace this with the IP you want to block. You can also add multiple IP addresses, each on a separate line such as:

deny from 192.168.20.10

deny from 194.157.32.21

Depending on what you want to block, you can even add this at a directory level. If you want to ban users or an ISP using the hostname, simply paste the following code in your .htaccess file:

order allow,deny 

deny from thebadguy.com 

allow from all

In brute force attacks, hackers use a network of bots to launch attacks on WordPress sites. Each bot in this network has an IP address assigned to it from a range of addresses. When your site is under attack, if you look at the bad IPs, you’ll notice a pattern, a range of IP addresses. It’s easy to block the whole range in one go instead of picking individual IPs and blocking them.

If you find modifying the .htaccess a daunting task then use a security plugin like Wordfence to ban an IP address.

Over to You

The .htaccess is a very powerful tool and modifying it can cause undesirable outcomes. Hence, taking backups is important so that when something goes wrong, you can simply restore the previous version of the site.

Thanks for reading!