Some people may wish to block the country due to different reasons. Certain countries are notorious for being home to spammers or hackers. cPanel comes with an IP blocking mechanism that helps protect your website from people you consider suspicious or even malicious. This is great for just one IP and even for a small number. However, it is not recommended in the event that you wish to block a country in its entirety with the .htaccess files.
Many business website owners are looking to block a country because they have no business in their country. In any event, you can put the code in the .htaccss file to block the IP addresses of certain countries. Follow the steps below to accomplish this task.
This simple and quick guide will help you to manage access to websites according to the location of visitors. With a few modifications made to the .htacess file, you can choose to permit or deny access to visitors from specific countries. Read on!
Step 1: Generate the Country’s IP Addresses
Table of Contents
Many online tools can be used to allow or deny visitors based on their country of residence. In this guide, we’ll be using country IP blocks.
- Visit the Country IP Blocks’ website ……or … The first step is to gather data. You’ll want to find an accurate list of IP addresses for the country you wish to block. If you search for the IP, you’ll discover that you can have your deny list created by IP2Location’s no-cost generator for visitor-blockers. This tool lets the user download a text file that contains the necessary code to block a particular country.
- Choose the countries you wish to allow or block. For instance, we chose randomly Algeria. The output format we picked for example is Apache .htaccess reject. Follow these steps to download the file.
- In the Select Format section, choose Apache .htaccess Deny or Apache .htaccess Allow. The selection is based on whether you’d like to restrict or block access from these countries.
- Click Create ACL. Create ACL Using Country IP Blocks
- Your ACL results will be displayed on the upper right side of the screen. Select the Copy ACL clipboard button. Allow or Block Countries Using Country IP Blocks
If you’re employing the function allowed, be sure to add deny at the top of the list of IPs that are allowed.
Second Step: Add the IP addresses generated by IP Addresses in the .htaccess File
Last, copy all the addresses for the nations you wish to restrict or block to the .htaccess file. Here’s how using Alavps cPanel
- Go to Files -> File Manager.
- Choose your domain, then click Go To File Manager.
- Go to the folder public_html and double-click on the .htaccess file.
- Paste the ACL in your .htaccess file and click Save & Close. The .htaccess files will be something like this: Allow or Block Countries with .htaccess File
- It is also possible to make use of the FTP client to modify this .htaccess file.
If you’re unable to access your website after putting in the IP addresses generated make sure the IP addresses you are using are in the blocked list. Find your IP address by using What is My IP Address.
Conclusion
In this article, you’ve learned a simple way to restrict or block visitors from certain countries. All you have to do is to create the IP address of your country using Country IP Blocks, and then include the access control list (ACL) in the .htaccess file.
We hope that this article helped you. We’d love to hear from you in the comments section if have any queries.