How to Allow or Block Visitors from Specific Countries Using .htaccess

How to Allow or Block Visitors from Specific Countries Using .htaccess

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.

  1. Scroll down to the bottom of the page and select a country from the drop-down menu.
  2. For the “Output Format”, select the Apache .htaccess deny. To get IPs to allow, you can select the Apache .htaccess allow.

  1. 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:

  1. Log into your hosting account and open the File Manager from the cPanel.

  1. 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”.

  1. A popup will show up to confirm. Please click on “Edit”.
  2. 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 27.116.56.0/22
deny from 43.230.209.0/24
deny from 43.231.131.0/24
deny from 43.249.40.0/22
deny from 43.250.136.0/22
deny from 45.65.58.0/23
deny from 45.116.128.0/23
deny from 45.125.224.0/22
deny from 45.126.253.0/24
deny from 45.150.95.0/24
deny from 57.92.0.0/20
deny from 58.147.128.0/19
deny from 59.153.124.0/22
deny from 61.5.192.0/20
deny from 62.4.115.0/24
deny from 64.86.63.64/27
deny from 64.207.208.0/21
deny from 65.216.143.24/29
deny from 69.88.10.160/28
deny from 74.118.80.0/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 27.116.56.0/22
allow from 43.230.209.0/24
allow from 43.231.131.0/24
allow from 43.249.40.0/22
allow from 43.250.136.0/22
allow from 45.65.58.0/23
allow from 45.116.128.0/23
allow from 45.125.224.0/22
allow from 45.126.253.0/24
allow from 45.150.95.0/24
allow from 57.92.0.0/20
allow from 58.147.128.0/19
allow from 59.153.124.0/22
allow from 61.5.192.0/20
allow from 62.4.115.0/24
allow from 64.86.63.64/27
allow from 64.207.208.0/21
allow from 65.216.143.24/29
allow from 69.88.10.160/28
allow from 74.118.80.0/22

This is an example. IP ranges will be different, based on the country you select.

Conclusion

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.

How to use .htaccess file to customize or improve a website

The .htaccess file plays an important role in the management and security of a w
5 min read
Amos Kingatua
Amos Kingatua
Hosting Expert

How To Fix The “Is Its Parent Directory Writable By The Server”?

Most of the times when you migrate a site from one hosting company to another an
3 min read
Arvind Singh
Arvind Singh
Hosting Expert

How to Secure the .htaccess File from Unauthorized Access

This guide will help you add one more security layer to your website by protecti
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Configure Nginx and Apache on the same Ubuntu VPS or Dedicated Server

Nginx and Apache are great and powerful web servers. However, they both have dra
2 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top