How to Allow or Block Visitors by Countries Using .htaccess

How to Allow or Block Visitors by Countries Using .htaccess

As the owner of a website, you’d want to allow visitors from all over the world to access your website. But if you noticed that you are being attacked by a specific country like from China, Vietnam or it can be from any country, you can’t allow them to access your website.

And also it makes no sense if you’re running local e-shop and you’re allowing other countries visitors including spammy bots to access your site. Better is to block them temporarily or permanently from accessing your website.

Now there are tons of reasons to Allow or Block Visitors from specific countries, let’s focus on the actual topic is how to block or allow visitors from countries using .htaccess.

So this tutorial will show how to use .htaccess to block IPs of visitors from the specific country.

As I said in this article “Best WordPress Plugins to Block Countries”, there are many ways to block visitors from specific countries like using a plugin, .htaccess, cPanel IP blocker, etc.

But by using .htaccess, you’ll need the whole bunch of IP addresses from a country.

Like if you made mind to block Vietnam from accessing your website then you’ll all IP’s list from Vietnam (Vietnam Population: 9.55 crores).

Now the main question is How to get these IPs so you can block them?

How to Get IPs List to Block Countries or Continents

You can generate a list of IP addresses from a specific country by using this website.


Follow the below steps to generate IPs list:

  1. Select a country that you want to either block or allow.
  2. Select IP version
  3. And Select output format

Once you select, hit the download button to get the whole bunch of IPs list from the selected country.

From that, you’ll get the IPs list that you need to paste in .htaccess file.

Now, you may be wondering these IPs are valid or up-to-date?

So yeah these IPs are valid and up-to-date but it can invalid and out-of-date if you don’t update this list every month.

Copying generated list into .htaccess file

Yes, you get IPs list from a country that you want to block. Now one more step to block these IPs to access your website.

.htaccess file is located in your website’s public_html directory. You can open it using FTP client software or File Manager in your cPanel.

Note: .htaccess might be hidden in public_html directory so check if Show Hidden Files is enabled in your File Manager settings:

Enable showing hidden

Once you located this file, right-click to edit the file and paste as it is a list that we generated earlier.

That’s it, you have learned how to block or allow visitors by a specific country using .htaccess.

Thanks for Reading

Enjoyed this post? Share it with your networks.