cPanel has an IP blocking mechanism to help you secure your site against individuals you consider suspicious or malicious. This works well for a single IP or even a handful. However, it’s not advisable if you want to block an entire country using your .htaccess file.
People may want to block a country for different reasons. Some countries are more known for having hackers or spammers. Other people want to block a country simply because they don’t do business with them. In any case, you can configure code inside the htaccess file to block the IP ranges of certain countries.
Follow the instructions below to perform this task.
Verifying the Existence of the .htaccess File:
Before you begin, make sure that the .htaccess file is present in the root directory of your website. If you don’t find it, it may be hidden.
How to Show Hidden Files in cPanel:
If the .htaccess file is not visible, follow these steps to show hidden files in cPanel:
Access the File Manager from your cPanel control panel.
Look for the “Settings” category in the top right corner of the File Manager window.
Select the option “Show hidden files (dotfiles).”
You should now be able to view and edit the .htaccess file in the correct directory.
Continue with the Country Blocking:
Once you’ve ensured the visibility of the .htaccess file, continue with the steps in the guide.
Blocking a Country from Accessing Your Site Using the .htaccess File:
First, you’ll need to gather the data. You’ll want to find a reliable list of IPs for the country you want to block. By searching, we discovered that you can get your denial list generated in the free IP visitor block generator from IP2Location. Using this tool will allow you to download a text file with the code needed to block a country — for example, Poland:
How to Generate the Country’s IP Addresses:
Go 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.
The tool will download the Access Control List (ACL) to your computer.
If you are using the allow function, make sure to insert the deny line below the list of allowed IPs.
Note: If you can’t find the IP address for a specific set of countries, sites like countryipblocks.net offer this data for a fee.
Open your file in the text editor of your choice and highlight and copy the information into the file.
Then, log in to your cPanel to access the .htaccess file.
Find the “Files” category and click the File Manager icon.
cPanel File Manager
You will then see your listed files; make sure you are set to show hidden files.
Navigate to the Document Root directory (folder) of your website, locate the .htaccess file and right-click on it. This will display a menu. Find and click the Edit option.
Editing the .htaccess file with the cPanel File Manager
You are now in the File Manager’s file editor. Paste your code into your .htaccess file to force the visitor to download that type of file. The code should have a format similar to the following but be much longer. (Some countries like China and the United States may have thousands of lines in their blocking code.)
Make sure to click the Save Changes button in the top right corner to save your new .htaccess configuration. Now, any IPs from those ranges will be denied access to your site.
Important: If you cannot access your website after inserting the generated IP addresses, check if your IP address is included in the block list and remove it. You can find your IP address through: What is my IP address.
Conclusion:
With this guide, you have learned the easy way to block or allow visitors from specific countries. All you need to do is generate the country’s IP address through IP2Location, and then insert an ACL into your .htaccess file.
Don’t hesitate to contact our technical support team via email at [email protected] or through live chat from your client area for additional assistance.