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>
deny from 22.214.171.124/24
deny from 126.96.36.199/22
deny from 188.8.131.52/29
deny from 184.108.40.206/19
deny from 220.127.116.11/31
deny from 18.104.22.168/31
deny from 22.214.171.124/32
deny from 126.96.36.199/24
deny from 188.8.131.52/24
deny from 184.108.40.206/24
deny from 220.127.116.11/29
allow from all
- 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.