In this article we will see How to Block IP from Specific Countries Using .htaccess file. 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 File
Once you had launched a website it will be available worldwide. However, in many cases, it’s necessary to control your site access to specific countries, especially when you want to stop spammers or hackers from particular countries.
For such cases you can use your WordPress .htaccess file to allow or block IP of visitors from specific countries. Let’s learn how to do so.
Step 1: Generate the Country’s IP Addresses
There are plenty of online tools available to allow or block visitors from specific country. In this tutorial, we’ll be using IP2Location.
- Head to the IP2Location Firewall IP list tool.
- Select the country you want to block or allow.
- Choose between IPv4 and IPv6 in the IP Version menu.
- Choose the format in the Output Format menu. The choice depends on whether you want to block or allow visitors from those countries.
- Click Download.

Step 2: Insert the Generated IP Addresses into the .htaccess File to Block IP
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.
- Open the ACL file on your computer, paste it to your .htaccess file, and hit Save. Your .htaccess file will look like this:
You can also use an FTP client to edit the .htaccess file.
Conclusion
In conclusion, Block IP access to your website from specific countries using .htaccess
and Apache’s mod_geoip module can be an effective way to enhance security and mitigate risks associated with malicious activities originating from certain geographical locations. By following the steps outlined in this guide, you can implement country-level Block IP seamlessly.
While implementing country-level blocking can enhance security, it’s essential to keep a few considerations in mind:
- Periodically Update GeoIP Database: Regularly update your GeoIP database to ensure accuracy in country-level identification.
- Limitations of Blocking by Country: Blocking by country is not foolproof, and it’s possible for users to bypass such restrictions using proxies or VPNs.
- Impact on Legitimate Users: Be cautious about the potential impact on legitimate users who may be accessing your website from the blocked countries.
By considering these factors and implementing additional security measures as needed, you can effectively leverage .htaccess
and Apache’s mod_geoip module to enhance the security of your website.