Many countries seem suspicious to many people, and most people don’t want visitors from those sites. So, they decide to block visitors from specific countries using .htaccess.
To block visitors, you have to find the IP range of that particular country to add it to the .htaccess file. The same applies when you want to allow visitors from a specific country.
You can either use FTP or the cPanel to access the .htaccess file. I prefer cPanel over FTP.
Finding IP Ranges for a Country
You might be thinking about using a built-in tool that’s on your hosting account. Most companies don’t have it. So, you can use a free IP2Location’s tool.
- Scroll down to the bottom of the page and select a country from the drop-down menu.
- For the “Output Format”, select the Apache .htaccess deny. To get IPs to allow, you can select the Apache .htaccess allow.
- Click on the download button, and you will have a text file on your computer. When you open it, it will consist of all IP ranges you can add to block visitors from.
Use the .htaccess File to Add IPs and Block Visitors.
Now, you have to edit the .htaccess file.
Here are a few steps to follow:
- Log into your hosting account and open the File Manager from the cPanel.
- Click on public_html from the sidebar to see all files and folders of your site. At the top, you will see folders. Scroll down and find the .htaccess file. Right-click and choose to “Edit”.
- A popup will show up to confirm. Please click on “Edit”.
- To the file, add the IP ranges that you downloaded from IP2Location. The text file will have the exact code to add.
Here is an example:
deny from 22.214.171.124/22 deny from 126.96.36.199/24 deny from 188.8.131.52/24 deny from 184.108.40.206/22 deny from 220.127.116.11/22 deny from 18.104.22.168/23 deny from 22.214.171.124/23 deny from 126.96.36.199/22 deny from 188.8.131.52/24 deny from 184.108.40.206/24 deny from 220.127.116.11/20 deny from 18.104.22.168/19 deny from 22.214.171.124/22 deny from 126.96.36.199/20 deny from 188.8.131.52/24 deny from 184.108.40.206/27 deny from 220.127.116.11/21 deny from 18.104.22.168/29 deny from 22.214.171.124/28 deny from 126.96.36.199/22
From the text file, you have to copy these IPs and add them to the .htaccess file and click on “Save Changes” from the top-right corner.
You may be wondering about how the code looks for allowing visitors from a specific country. The only difference is that you replace “deny” to “allow”.
Here it is:
allow from 188.8.131.52/22 allow from 184.108.40.206/24 allow from 220.127.116.11/24 allow from 18.104.22.168/22 allow from 22.214.171.124/22 allow from 126.96.36.199/23 allow from 188.8.131.52/23 allow from 184.108.40.206/22 allow from 220.127.116.11/24 allow from 18.104.22.168/24 allow from 22.214.171.124/20 allow from 126.96.36.199/19 allow from 188.8.131.52/22 allow from 184.108.40.206/20 allow from 220.127.116.11/24 allow from 18.104.22.168/27 allow from 22.214.171.124/21 allow from 126.96.36.199/29 allow from 188.8.131.52/28 allow from 184.108.40.206/22
This is an example. IP ranges will be different, based on the country you select.
Many of my clients are always worried about getting visitors from countries like China, Algeria, Afghanistan, and many others. So, they need to allow and block visitors from specific countries.
Thanks to WordPress for giving such features by using the .htaccess file. I am sure; you can easily figure it out and block suspicious countries.
- Want info about best web hosting? Clicking this link can be of great help.