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

Our quick and easy tutorial will show you how to control site access based on visitors’ location. With some tweaks to the .htacess file, it’s possible to allow or block visitors from certain countries. Keep reading!

Upgrade your web hosting today with Hostinger. Find the perfect package for your personal website!

Get Your Discount Here

Allowing or Blocking Visitors from Specific Countries Using .htaccess

Once launched, a website is available worldwide. But in many cases, it’s necessary to control your site access, especially when you want to stop spammers and hackers from particular countries.

Fortunately, you can use your WordPress .htaccess file to allow or block visitors from specific countries. Let’s learn how to do so.

Step 1: Generate the Country’s IP Addresses

Plenty of online tools are available to allow or block visitors by the country. In this tutorial, we’ll be using Country IP Blocks.

  1. Head to Country IP Blocks homepage.
  2. Select the countries you want to block or allow.
  3. For the Select Format section, choose Apache .htaccess Deny or Apache .htaccess Allow. The choice depends on whether you want to block or allow visitors from those countries.
  4. Click Create ACL.Create ACL Using Country IP Blocks
  5. The ACL results will appear on the right side of your screen. Press the Copy ACL to Clipboard button.Allow or Block Countries Using Country IP Blocks

Step 2: Insert the Generated IP Addresses into the .htaccess File

Finally, paste the IP addresses of the countries you want to block or allow to .htaccess file. Here’s how to do it using Hostinger’s hPanel:

  1. Go to Files -> File Manager.
  2. Select your domain and hit Go To File Manager.
  3. Navigate to the public_html folder and double-click the .htaccess file.
  4. Paste ACL to your .htaccess file and hit Save & Close. Your .htaccess file will look like this:Allow or Block Countries Using .htaccess File

You can also use an FTP client to edit the .htaccess file.

If you cannot access your website after inserting the generated IP addresses, check whether your IP address is included on the block list. You can see your IP address via What Is My IP Address


In this tutorial, you’ve learned the easy way to block or allow visitors from specific countries. All you need to do is generate the country’s IP address via Country IP Blocks, then insert an access control list (ACL) into your .htaccess file.

We hope that this simple guide is helpful. Let us know in the comments if you want to further discuss this topic.

The Author


Elvinas S. / @elvinas

Elvinas is a senior server administrator at Hostinger. He monitors the infrastructure’s well-being and keeps the uptime at a maximum. Besides server management, web development has always been one of his biggest passions.

Related tutorials


Zinia Tabassum Reply

March 07 2020

is it possible to restrict mails from a specific country?


Zinia Tabassum Reply

March 07 2020

is it possible to restrict emails from a specific country?



    Replied on March 12 2020

    Hi Zinia, Yes, it is! Follow this tutorial ( Good luck!


Dave Reply

April 30 2020

Thank you for the tutorial and link to the country IP tool! it's been a HUGE help on reducing spam to my sites! I'll be curating my clients sites with this as well. Thank you again!


Heiko Reply

August 17 2020

Thanks for this helpful tutorial! Great article! Greetings from Germany!


Vysakh V J Reply

December 11 2020

By using these code if i block IP's from united states traffic, is it affect crawling of my website from google bot?



    Replied on February 09 2021

    Hi! The main Googlebot is visiting your site with a California IP address. However, there are versions of 'localized" bot - check here One thing to bear in mind - the 'localized' bot might crawl your website as often as the main Googlebot (with California IP). So, blocking all US traffic would be have it's consequences. By the way, if you decided to allow access only to the bot and block all other US traffic, that would be cloaking, which is against Google policy. So we highly advice not to do so.

Leave a reply




This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in htaccess
How to Locate and Create .htaccess File – A Step-by-Step Guide