Dec 23, 2022
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.
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 Country IP Blocks.
- Head to Country IP Blocks homepage.
- Select the countries you want to block or allow.
- On 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.
- Click Create ACL.
- The ACL results will appear on the right side of your screen. Press the Copy ACL to Clipboard button.
- 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:
- Go to Files -> File Manager.
- Select your domain and hit Go To File Manager.
- Navigate to the public_html folder and double-click the .htaccess file.
- Paste ACL to your .htaccess file and hit Save & Close. Your .htaccess file will look like this:
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
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 guide was helpful. Let us know in the comments if you have any questions.
March 07 2020
is it possible to restrict mails from a specific country?
March 07 2020
is it possible to restrict emails from a specific country?
March 12 2020
Hi Zinia, Yes, it is! Follow this tutorial Good luck!
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!
August 17 2020
Thanks for this helpful tutorial! Great article! Greetings from Germany!
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?
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.
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.
September 21 2021
Hi Peter! Thanks for pointing that out, our team is already working on updating this in the article :)
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
March 01 2022
Hi there! When allowing an IP, make sure to have the line
deny from allto block the rest of the traffic (step 6). Let me know how it goes for you!
June 23 2022
Where do i add the line "deny from all"? At the beginning? At the end? Sorry, i'm a tech-noob :(
July 01 2022
Hey there! No worries! You can add the code to block IP addresses from a specific country near the end of your.htaccess file, under any other entries in the file 😊
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.
November 18 2022
Hey! If you limit access to your website from a specific country, any visitor from that country will get an error while accessing your URL, website might still be visible in Google searches based on website's indexing, however it will have no affect to website's SEO, so your rankings in the allowed countries would not suffer.