How to Block Visitors by Country Using .htaccess: A Step-by-Step Guide

When you own a website, you often want to allow visitors from everywhere. But sometimes, it’s necessary to block visitors from specific countries, such as when hosting a local contest or managing website traffic. Blocking countries can also help reduce bot activity and unwanted traffic on your site.

The easiest way to block or allow visitors from certain countries is through .htaccess without needing complex server configurations. You can do this with FTP and country-specific IP lists in CIDR format.

Important: Regularly update your IP lists, as IP address databases are constantly changing, and outdated lists can accidentally block legitimate traffic.


1. IP2Location

IP2Location has been a trusted provider of geolocation databases since 2002. They offer a free firewall database that lets you block or allow specific countries. You can generate the block list by selecting IPv4, the country (or countries), and choosing “Apache .htaccess deny” or “allow” from the menu.

Once generated, download the text file and upload it to your website’s root directory. Rename the file to .htaccess after uploading, as Windows doesn’t support that file extension.

IP2Location Block List

Note: The free database may be limited and isn’t always up-to-date.

Visit IP2Location


2. Country IP Blocks

Country IP Blocks offers a similar service, providing both premium and free GeoIP databases. You can select the countries you want to block or allow and then generate an access control list for .htaccess.

Country IP Blocks

The free version may be 30–60 days old, while premium users get access to the most recent data.

Visit Country IP Blocks


3. BlockACountry.com

BlockACountry.com is useful for handling country blocking for a few websites. After signing up for a free account, you can create a block list by selecting a website and a country to block. You can edit your selections at any time by logging into your profile.

BlockACountry

Visit BlockACountry.com


4. IPdeny

IPdeny offers downloadable country-specific IP blocks, though their online firewall generator is no longer available. To use their data, download the country zone file, open it in Notepad++ or WordPad (avoid Notepad), and add the appropriate code to make it compatible with .htaccess.

Here’s a sample modification for Apache compatibility:

pgsqlCopyEdit<Limit GET HEAD POST>
order allow,deny
allow from all

After adding the IP blocks, close the list with </Limit>.

Add Prefix to IP Blocks

Visit IPdeny


5. Country IP Range from RIPE

For updated IP ranges, Ivan Erben’s Python script fetches daily IP data from RIPE (the official IP address administrator). Although you’ll need to modify the output to fit .htaccess, the script offers accurate, up-to-date data.

Download Country IP Range from RIPE


6. Software77 IP to Country Database

Software77 offers a free IP-to-country database. Choose the country you want, select CIDR format, and submit the form to generate the IP blocks. As with the others, you’ll need to convert it for .htaccess.

Software77 IP to Country

Visit Software77 IP to Country Database


7. LUDOST.NET

LUDOST.NET collects IP data from various sources, including RIPE, and provides it in several formats compatible with Apache, Nginx, and iptables. You can select countries by their two-letter codes and generate an access control list for .htaccess.

LUDOST Country to IP Database

Visit LUDOST.NET


By following these steps, you can efficiently block or allow visitors from specific countries using .htaccess. Each method comes with its pros and cons, so it’s essential to choose the best fit for your needs. Always ensure your IP lists are updated regularly for the best results.

20 Comments - Write a Comment

  1. KT Lee 3 years ago
  2. noda 4 years ago
  3. Hazel 5 years ago
    • Moudifruity 5 years ago
      • Moudifruity 5 years ago
  4. CloudiBee 6 years ago
  5. Ravi Sah 7 years ago
  6. arash 7 years ago
  7. Chris Huey 8 years ago
  8. Celine 8 years ago
  9. Shimul Sarker 8 years ago
  10. jon 9 years ago
  11. Michelle 10 years ago
  12. Johny 10 years ago
  13. Ivan Erben 10 years ago
  14. Asma 15 years ago
  15. Anadi Chaturvedi 15 years ago
  16. Zakir 15 years ago
  17. prasad 15 years ago
  18. Ishaan 15 years ago

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: Your comment is subject to approval. Read our Terms of Use. If you are seeking additional information on this article, please contact us directly.