Security and SSL

Block or allow access by country

Reviewed on 6 min read firewall country blocking

You can limit visits from certain countries in two ways: from the client area, without touching any file, or with rules in the .htaccess when you need something more specific, such as protecting only the admin login or redirecting by country.

The country is worked out from the visitor's IP. It is reliable in most cases, but anyone using a VPN or a proxy will appear with the country of that service.

From the client area

  1. Log in to the client area and open your hosting service.
  2. Go to Firewall and antivirus > Geo-blocking.
  3. Select the domain you want to protect.
  4. Choose the mode:
    • Blacklist: blocks the selected countries and allows the rest.
    • Whitelist: allows only the selected countries and blocks the rest.
  5. Select the countries (or entire continents) and save.

The block applies to the whole domain. If you use a whitelist, check that you are not shutting out services that connect to your site from other countries, such as payment gateways, webhooks or uptime monitors. To remove it, go back to the same section and remove the configuration.

With rules in the .htaccess

The server geolocates each visit and leaves the result in the variables GEOIP_COUNTRY_CODE (country) and GEOIP_CONTINENT_CODE (continent), which you can use in the domain's .htaccess. Add the rules at the beginning of the file. If you also use the client area block, do not edit the block between # BEGIN HostsuarGeoBlock and # END HostsuarGeoBlock: the panel rewrites it when you save.

Countries are given by their two-letter ISO 3166-1 alpha-2 code: ES Spain, RU Russia, CN China, US United States… Continents use EU Europe, AS Asia, AF Africa, NA North America, SA South America and OC Oceania.

Block several countries

This example blocks Russia, China and North Korea and allows the rest:

GeoIPEnable On
SetEnvIf GEOIP_COUNTRY_CODE RU DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE CN DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE KP DenyCountry
Allow from all
Deny from env=DenyCountry

Add one SetEnvIf line for each country you want to block.

Allow only some countries

The other way round: allow Spain and Portugal and block the rest:

GeoIPEnable On
SetEnvIf GEOIP_COUNTRY_CODE ES AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE PT AllowCountry
Deny from all
Allow from env=AllowCountry

To allow an entire continent, use GEOIP_CONTINENT_CODE; for example, Europe only:

GeoIPEnable On
SetEnvIf GEOIP_CONTINENT_CODE EU AllowCountry
Deny from all
Allow from env=AllowCountry

Limit wp-admin access to your country

Often you do not want to block the site, only the admin login. This example lets people into WordPress's wp-login.php, wp-admin and xmlrpc.php only from Spain; the rest of the site stays open to everyone:

GeoIPEnable On
RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(ES)$
RewriteCond %{REQUEST_URI} ^/(wp-login\.php|wp-admin/|xmlrpc\.php) [NC]
RewriteCond %{REQUEST_URI} !^/wp-admin/admin-ajax\.php [NC]
RewriteRule ^ - [F,L]

The admin-ajax.php exception is needed because many plugins and themes also use it on the public side. To allow more than one country, separate them with |: !^(ES|PT)$. If you travel, remember that you will be locked out too until you remove or widen the rule.

Redirect by country

You can also send visitors from certain countries to another version of the site. This example redirects anyone arriving from the United States or Canada:

GeoIPEnable On
RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(US|CA)$
RewriteRule ^(.*)$ https://www.otraweb.com/$1 [R=302,L]

Use 302 (temporary) and not 301: browsers store permanent redirects, and a visitor who changes country would still be redirected. Bear in mind that Google crawls mostly from the United States, so a redirect by country can stop it from seeing your original site.

Read the visitor's country from PHP

With GeoIPEnable On at the beginning of the .htaccess, your PHP code receives the geolocation in $_SERVER:

<?php

echo $_SERVER['GEOIP_ADDR'];           // IP the visitor connects from
echo $_SERVER['GEOIP_COUNTRY_CODE'];   // country code, for example ES
echo $_SERVER['GEOIP_CONTINENT_CODE']; // continent code, for example EU

That way you can, for example, preselect the language or currency without calling an external service. If the IP cannot be located, the variable does not exist: use $_SERVER['GEOIP_COUNTRY_CODE'] ?? '' to avoid notices.

If a legitimate visitor can no longer get in

  • Check the country assigned to their IP with any geolocation service and compare it with your list.
  • In a whitelist, adding a country allows it; in a blacklist, it blocks it. Check the mode before adding countries.
  • If your site is behind a CDN such as Cloudflare, the server may see the CDN's IP instead of the visitor's. In that case, use the CDN's own country blocking.
  • If you have blocked yourself, remove the rules from the .htaccess using the cPanel File Manager, or remove the block from the client area.

Country blocking greatly reduces automated malicious traffic, but it is no substitute for keeping your site up to date and using strong passwords.

You may also find useful

Something wrong or out of date? Tell us and we will review it.