How to Block Country Traffic in cPanel Using
.htaccess (Enhanced Guide)
Controlling who accesses your website based on their geographic location
can be a powerful way to improve security, reduce unwanted traffic, and optimize
performance. Whether you want to prevent malicious access from certain regions or simply
streamline your server load, country-based IP filtering via .htaccess in cPanel is an
effective and accessible solution.
In this enhanced tutorial, we’ll walk you through a step-by-step method to block or allow
visitors from specific countries using .htaccess—a configuration file used by Apache web
servers.
Why Block Country Traffic?
Blocking country traffic can be useful in situations such as:
- Preventing malicious traffic from regions known for high spam or attack rates.
- Improving website speed for your core audience by limiting global traffic.
- Restricting access to region-specific content or services.
- Complying with legal or regulatory restrictions that apply to specific
locations.
Step-by-Step Guide to Block or Allow Country
IPs via .htaccess in cPanel
Step 1: Generate a List of IP Addresses for the Country
To block or allow visitors by country, you need the list of IP address
ranges assigned to that region. Follow these steps:
1.1 – Visit a Country IP Lookup Tool
Go to a reliable IP database provider like IP2Location Visitor Blocker Tool. You can
also use alternatives like Country IP Blocks, GeoIPTools, or software-based IP
Geo-location databases.
1.2 – Select the Country
From the tool, choose the country you want to block (or allow). You’ll typically see
options to export the IP addresses in different formats.
1.3 – Choose IPv4 or IPv6
For most websites, IPv4 is sufficient, but if your site serves users with newer IPv6
connections, you may want to include both.
1.4 – Select the Output Format
Choose “Apache .htaccess Deny” if you plan to block traffic or “Allow” if you want to
restrict access to only specific countries. This generates code that is ready to be
pasted into your .htaccess file.
1.5 – Download the List
Click to generate and download the list. For allow lists, be sure to add deny from all
above your allow rules to block everything except the specified countries.
Step 2: Insert the IP Rules into Your .htaccess File
2.1 – Open File Manager in cPanel
Login to your cPanel dashboard, and go to Files → File Manager.
2.2 – Locate Your .htaccess File
Navigate to your public_html directory (or your specific website root). Make sure hidden
files are visible—enable the option in File Manager settings if necessary.
2.3 – Edit the .htaccess File
Right-click on .htaccess and select Edit. Paste the generated list in this format:
# Block Visitors from [Country Name]
<Limit GET POST>
order allow,deny
allow from all
deny from 192.168.0.0/24
deny from 203.0.113.0/24
# Add additional IPs or ranges as needed
</Limit>
For allow rules
, structure it like this:
<Limit GET POST>
order deny,allow
deny from all
allow from 192.168.1.0/24
allow from 198.51.100.0/24
# Add more IPs as necessary
</Limit>
2.4 – Save and Test
Click Save Changes, then visit your site or use a VPN to verify that IP blocking or allowance is working as expected.
Alternative Methods
If you’re looking for a more scalable or GUI-based approach, consider:
- Cloudflare firewall rules (easier management, but on paid plans).
- ConfigServer Security & Firewall (CSF) – Available on cPanel with root access.
- ModSecurity with GeoIP2 integration– Advanced option for experienced admins.
Conclusion
Blocking or allowing traffic by country using the .htaccess file is a smart, code-light way to take control over your website’s audience and security. Whether you want to defend against unwanted bots or enforce region-specific restrictions, this method gives you server-level access control without needing advanced firewall configurations.
Just remember: geolocation by IP isn’t 100% accurate, and maintaining your rules regularly is key to keeping your access control effective.