What is mod_geoip?

What is mod_geoip?

You can add modules to Apache servers on VPS and Dedicated accounts for extra functionality. One such module is mod_geoip. mod_geoip is a way for Apache to determine the location of your site’s visitors IP address. Knowing the country of origin for a visitor can prove useful in certain situations. For example, you may want to redirect visitors from a certain country to a version of your site that is in their native language.

How do I use mod_geoip?

Setting up mod_geoip requires two steps: enabling mod_geoip and adding code to your .htaccess.

Enabling mod_geoip

You can enable mod_geoip in your VPS or Dedicated Account Control Center (ACC). To enable, follow these steps:

  1. Log in to the ACC on your VPS or Dedicated account
  2. In the left sidebar, click Dedicated, then click Apache Options in the drop-down
  3. In the Apache Module section, check the box next to mod_geoip
  4. Click the Make Changes button

This will enable mod_geoip on your server, though it may take up to 15 minutes for the change to go into effect.

Add Code to Your .htaccess

After mod_geoip has been enabled, the next step is to add directives to your .htaccess file. For information about accessing your .htaccess, see our Accessing Your .htaccess File article. The code you add here will determine how the knowledge of the country of origin is used.

For each of these codes, you will need to specify the country. You can do this by using the country codes that have been established for each country. You can find a list of country codes at ip2country.net.

Redirect One Country

You can redirect one country to a specific site or page. To do this, enter the following code into your .htaccess file:

#Redirect one country to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://www.example.com$1 [R,L]

Replace the CA after GEOIP_COUNTRY_CODE with the country code for the country you would like to redirect. 

Redirect Multiple Countries to a Single Page

You can also redirect multiple countries to a specific site or page. To do this, enter the following code into your .htaccess file:

# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ http://www.example.com/na/$1 [R,L]

Replace the country codes CA, US, and MX after GEOIP_COUNTRY_CODE with the country code for the country you would like to redirect.

Block Traffic from a Country

Another option is to block traffic from a specific country. Enter the following code into your .htaccess file:

#Block Traffic from countries
SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry

#add additional blocked countries here
Deny from env=BlockCountry

Replace the country codes CN and RU after GEOIP_COUNTRY_CODE with the country code for the country you want to block. You can add additional countries that should be blocked by repeating the following code underneath the original lines:

SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry

Be sure to add the country code to the new lines.

Allow Only Countries Specified

You can also specify that only certain countries can access your site and all others will be blocked.

# Allow only countries specified
SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE MX AllowCountry

#add additional accepted countries here
Deny from all
Allow from env=AllowCountry

Replace the country codes US, CA, and MX after GEOIP_COUNTRY_CODE with the country code that should be given access. If you want to add more than one accepted country, you can repeat the following lines underneath the original lines:

SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE MX AllowCountry

Be sure to add the country code to the new lines.

Updated on September 17, 2018

Was this article helpful?

Related Articles