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.
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.