1. Home
  2. Security & Abuse
  3. How to Block Specific Countries from Accessing your Site

How to Block Specific Countries from Accessing your Site

This one is pretty specific!

The following article will really only help you if your website is hosted on a Litespeed HTTPD server, with the Geo-location database configured and enabled. If you’re on Hostupon shared hosting services, this is for you!

If you want to block one or more countries from being able to access your website, there are several methods available to you, but many of them involve adding entire IP ranges to your .htaccess file and this can get clunky. Also, IP ranges often change so unless you keep your IP list updated it can fall behind or start blocking traffic it shouldn’t.

Instead, since our servers use Litespeed for the webserver, you can use its built-in systems for geographic filtering.

Start by adding a blank ‘blocked.html’ file to the root of your website. This is the file we’ll send blocked countries to, and it prevents WordPress and other scripts from trying to process error codes (like 403, 404, etc) for these regions. You can use the cPanel file manager to create this new file.

This file can be customized with any HTML you like, so if you want it can be made to match the theme of your website, or left blank. Just be aware that users looking at this file due to being blocked won’t be able to access other resources (including images, css, or javascript) unless you carve out exceptions for them in the rewrite rule.

Once that’s done, use the File Manager again and find your .htaccess file (It’s in the root of your website) and add this code to the beginning of it:

GeoIpEnable On
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\/blocked\.html$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(HK|CN|SG|BR)$
RewriteRule .* /blocked.html [R=301,L]

In this particular example, this will block Hong Kong (HK), China (CN), Singapore (SG), and Brazil (BR) but you can customize this list however you like. Just make sure to put a pipe character (“|”) between each two-letter code.

For a full reference to all countries and their codes, see this Wikipedia article.

Updated on September 9, 2025

Was this article helpful?

Related Articles