How to block or allow countries using GeoIP and .htaccess Print

  • 26

How to use .htaccess to block countries from your website
FatCat Servers uses GeoIP to look up the IP address of the client end user. GeoIP sets the variable GEOIP_COUNTRY_CODE we will use this code the in the examples below to block or allow access to a website. 

Edit the .htaccess in the root folder usually thes is /public_html. Tip: If the file does not exist you can create one - Click to See how

Example: Deny Access to specified countries

The following .htaccess will Deny traffic from China, North Korea, South Korea
GeoIPEnable On
# Put countries to deny here      
SetEnvIf GEOIP_COUNTRY_CODE CN DenyCountry  
SetEnvIf GEOIP_COUNTRY_CODE KR DenyCountry  
SetEnvIf GEOIP_COUNTRY_CODE KP DenyCountry  
  
Allow from all  
Deny from env=DenyCountry    
Example: Allow Access from specified countries

NOTE: Blocking is usually prefered to Allowing - The reason is if an user is behind a proxy or their location cannot be determined for any reason this method will block them.

The following .htaccess will only allow traffic from the US, Canada and Mexico
GeoIPEnable On
# Put countries to allow here      
SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry  
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry  
SetEnvIf GEOIP_COUNTRY_CODE MX AllowCountry  
  
Deny from all  
Allow from env=AllowCountry    

Was this answer helpful?

« Back