Once you launch your website, it is immediately available to most of the world. The Internet is, by default, a global place for commerce and information. Still, as the owner and operator of your website, you may wish to disallow specific countries to have access to your site. The biggest reason people want to block a country is due to a high number of attacks coming out of it. You also may simply not want to do business with a specific country.
Whatever your reason, you can block a country by editing the htaccess file and setting it to deny its IP ranges. Below are the instructions for performing this task.
How to block a country's IP ranges in .htaccess
- The first thing we need are the IP ranges you wish to block. A visitor-blocker generator is available at IP2Location.com. This tool is free to use. It allows you to create a text file with the code you will need. The country we chose for our sample is North Korea, due to its small number of IP ranges for demonstration. The output format chosen for the code is Apache .htaccess deny.
- Now that you have the file downloaded to your computer, you will need to log into the cPanel dashboard.
- Once inside the cPanel, enter the File Manager tool and locate your htaccess file for editing.
- Once you open the .htaccess file editor, paste the code for blocking the country inside at the top of the file. Below is an example of how the code looks for North Korea.
<Limit GET HEAD POST>
order deny,allow
deny from 5.224.126.0/24
deny from 175.45.176.0/22
deny from 67.43.236.32/29
deny from 57.73.224.0/19
deny from 46.36.203.84/31
deny from 46.36.203.82/31
deny from 46.36.203.81/32
deny from 37.223.126.0/24
deny from 37.222.126.0/24
deny from 5.225.126.0/24
deny from 209.28.38.0/29
allow from all
</Limit>
- Once you enter the code, click on the Save Changes button to activate the .htaccess and begin to block the country. Any IP addresses within these ranges will receive an access denied message.
We value your feedback!
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
new! - Enter your name and email address above and we will post your feedback in the comments on this page!