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

  1. 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.
  2. Now that you have the file downloaded to your computer, you will need to log into the cPanel dashboard.
  3. Once inside the cPanel, enter the File Manager tool and locate your htaccess file for editing.
  4. 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>
  5. 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.
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
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.
How did you find this article?
Please tell us how we can improve our Help Center:
Email Address
Optional, but our team may contact you for more information.
Did you find this article helpful?

Post a Comment

Name:
Email Address:
Comment:
Are you a bot?
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Would you like to ask a question about this page? If so, click the button below!

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.