Don’t miss the New Year’s Sale deals!
search
In this article

How to block IP by country: restrict access to your website from specific countries using .htaccess

This 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!

Allowing or Blocking Visitors From Specific Countries Using .htaccess

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

Hostinger web hosting banner

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 according to their country. In this tutorial, we’ll be using IP2Location.

  1. Head to the IP2Location Firewall IP list tool.
  2. Select the country you want to block or allow.
  3. Choose between IPv4 and IPv6 in the IP Version menu.
  4. Choose the format in the Output Format menu. The choice depends on whether you want to block or allow visitors from those countries.
  5. Click Download.
The interface of IP2Location IP list tool, showing the country, IP version, and output format menus.
  1. The tool will download the Access Control List (ACL) into your computer.
  2. If you are using the allow function, make sure to insert the line deny from all above the list of allowed IPs.

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 FilesFile 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. Open the ACL file on your computer, paste it to your .htaccess file, and hit Save. Your .htaccess file will look like this:
An .htaccess file containing an ACL

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

Important! 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 find your IP address via What Is My IP Address

Conclusion

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 IP2Location, then insert an ACL into your .htaccess file.

We hope that this guide was helpful. Let us know in the comments if you have any questions.

Hostinger web hosting banner
Author
The author

Elvinas S.

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.

What our customers say

Comments

Author
Zinia Tabassum

March 07 2020

is it possible to restrict mails from a specific country?

Author
Zinia Tabassum

March 07 2020

is it possible to restrict emails from a specific country?

Author
Dave

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!

Author
Heiko

August 17 2020

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

Author
Vysakh V J

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?

Author
Peter

July 20 2021

One detail left out. If you use the "Apache .htaccess Allow" on that page to allow access from ONLY one or a group of countries, the ACL code it spits out won't work. It's missing one tiny, necessary line. After generating the ACL code and copying it to clipboard in steps 4 & 5, first add the line "deny from all" to your .htaccess file, then underneath it paste in the code from the clipboard.

Author
biz

February 22 2022

I'm using method to allow some IP address. but it doesn't work. can you please give another method or tell my why this method not works for me

Author
Deniz

June 23 2022

Where do i add the line "deny from all"? At the beginning? At the end? Sorry, i'm a tech-noob :(

Author
Ajay

November 14 2022

Hi Elvinas, very useful information but i would like to one thing that if I am blocking traffic from specific country that means my website will not accessible from that specific country, right? and secondly by doing this is it my website will disappear from the blocked country search engine. For example: I have blocked UAE traffic so my website will not appear in Google UAE search engine right? & third thing is this affect my site ranking in other country search engine like Google USA? I would like to know your thoughts.

Author
Oleksandr

February 27 2024

Hello As I understand that hostinger uses ngix, but also allows to use htacces rules at same time? Is it possible to restrict access to some files in directory by mask? Thank you

Leave a reply

Please fill the required fields.Please accept the privacy checkbox.Please fill the required fields and accept the privacy checkbox.

Thank you! Your comment has been successfully submitted. It will be approved within the next 24 hours.