How to block or allow website visitors by country with .htaccess

Published Updated

On 2MHost LiteSpeed servers, you can allow or block website visitors by country with a few .htaccess rules. GeoLocation is included at no extra cost.

GeoLocation detects the visitor's country from their IP address. The LiteSpeed web server stores it as a two-letter code in the GEOIP_COUNTRY_CODE variable, which your .htaccess rules and PHP code can read.

You can use it to limit access to your whole website or to one folder, and to keep out unwanted or suspicious traffic. You do not need a paid security plugin, such as the premium version of Wordfence, to block countries. For more on Wordfence, see Do I need to use Wordfence on my WordPress website?

Before you begin

  • Your website must be on a server that runs the LiteSpeed web server. GeoLocation does not work on other servers.
  • Download a copy of your .htaccess file first. A typing error in this file can take your whole website offline, and the copy lets you restore it in seconds.
  • Know the two-letter codes of the countries you want to allow or block. GeoLocation uses ISO 3166-1 alpha-2 codes, for example US (United States), CA (Canada), GB (United Kingdom) and DE (Germany).

Open the .htaccess file in cPanel

You add GeoLocation rules to the .htaccess file in your website's folder, using the cPanel File Manager.

  1. Log in to the 2MHost client area.
  2. Click Hosting accounts, then click the cPanel button of your hosting account. cPanel opens already signed in.
  3. In cPanel, open Files › File Manager.
  4. Open the public_html folder, or the folder of the website you want to protect.
  5. Click Settings, select Show Hidden Files (dotfiles) and click Save. The .htaccess file appears in the list.
  6. Right-click .htaccess and click Download to keep a backup copy.
  7. Right-click .htaccess again and click Edit.
  8. Paste the rules from one of the sections below at the very top of the file, above any other rules such as the # BEGIN WordPress block.
  9. Click Save Changes.

Allow visitors from selected countries only

These rules in your .htaccess file let visitors from the United States and Canada open your website. Visitors from every other country get a 403 Forbidden error.

<IfModule mod_geoip.c>
  RewriteEngine On
  RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(US|CA)$
  RewriteRule .* - [F,L]
</IfModule>

I means OR, so US|CA means USA or Canda. Replace US|CA with the codes of the countries you want to allow, separated by |. Make sure your own country is in the list.

Warning: Google's crawler visits mostly from the United States. If you leave US out of the allowed list, Google can no longer read your website and your pages can drop out of search results.

Block visitors from selected countries

These rules in your .htaccess file let visitors from every country in, except the United States and Canada. Blocked visitors get a 403 Forbidden error.

<IfModule mod_geoip.c>
  RewriteEngine On
  RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(US|CA)$
  RewriteRule .* - [F,L]
</IfModule>

Replace US|CA with the codes of the countries you want to block, separated by |.

Redirect visitors by country

Instead of blocking visitors, your .htaccess file can send them to another website based on their country. The target must be a different website; a redirect to a page on the same site loops and breaks that page's images and styles.

Redirect one country to another website

These rules send visitors from Canada to the same page on another website, so /contact goes to https://www.example.ca/contact.

<IfModule mod_geoip.c>
  RewriteEngine On
  RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
  RewriteRule ^(.*)$ https://www.example.ca/$1 [R=302,L]
</IfModule>

Replace CA with the country code and www.example.ca with the website you want to send those visitors to.

Redirect several countries to one page

These rules send visitors from Canada, the United States and Mexico to a single page on another website.

<IfModule mod_geoip.c>
  RewriteEngine On
  RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
  RewriteRule ^ https://www.example.com/north-america/ [R=302,L]
</IfModule>

Replace CA|US|MX with your country codes and https://www.example.com/north-america/ with the full address of the target page.

Limit one folder by country

To limit only part of your website, put the rules in a .htaccess file inside that folder instead of in public_html. The rules then apply to that folder and everything in it.

  1. In cPanel, open Files › File Manager and open the folder you want to protect.
  2. Edit the .htaccess file in that folder, or click + File to create one named .htaccess.
  3. Paste the allow or block rules from the sections above and click Save Changes.

Read the visitor's country in PHP

On a LiteSpeed server, PHP code can read the visitor's country from $_SERVER['GEOIP_COUNTRY_CODE']. This also lets you check that GeoLocation works before you add any blocking rules.

  1. In cPanel, open Files › File Manager and open public_html.
  2. Click + File, name the file geotest.php and click Create New File.
  3. Right-click geotest.php, click Edit, paste the code below and click Save Changes.
  4. Open https://example.com/geotest.php in your browser, with your own domain instead of example.com. The page shows your country code, for example Your country code: US.
  5. Delete geotest.php when you finish testing.
<?php
$country = $_SERVER['GEOIP_COUNTRY_CODE'] ?? 'not detected';
echo 'Your country code: ' . htmlspecialchars($country);

If the page shows not detected, GeoLocation is not active for your website, and the .htaccess rules in this article have no effect.

Troubleshooting

"403 Forbidden" when you open your own website

Your own country is blocked, or missing from the allowed list. In cPanel File Manager, edit .htaccess and add your country code to the allowed list or remove it from the blocked list. If you use a VPN, turn it off, because the VPN's country counts instead of yours. If your connection is blocked by the server instead, see How to check if my connection is blocked by server firewall?

The rules have no effect

GeoLocation is not active for your website, or other rules run first. Check that your website is on a LiteSpeed server with the geotest.php test above, and move the GeoLocation rules to the very top of .htaccess.

"500 Internal Server Error" after you edit .htaccess

The file has a typing error, such as two rules on one line or a space inside the brackets ([R, L] instead of [R,L]). Upload your backup copy of .htaccess, or fix the line and save again.

"ERR_TOO_MANY_REDIRECTS" after you add a redirect

The redirect sends visitors to a page on the same website, so they are redirected again and again. Point the redirect to a different website, or remove the redirect rules.

Frequently asked questions

Does GeoLocation cost extra?

No. GeoLocation is included at no extra cost on 2MHost LiteSpeed servers.

Can visitors get around a country block?

Yes. A visitor who uses a VPN or proxy appears to come from the VPN's country. Country blocking reduces unwanted traffic, but it does not replace strong passwords and security updates.

Can I block a country only from my WordPress login page?

Yes, for the wp-admin folder: put the block or allow rules in a .htaccess file inside wp-admin, as described in the folder section above.

Related articles