What is mod_maxminddb?
You can add modules to Apache servers on VPS and Dedicated accounts for extra functionality. One such module is mod_maxminddb. mod_maxminddb 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_maxminddb?
Setting up mod_maxminddb requires two steps: enabling mod_maxminddb and adding code to your .htaccess.
Enabling mod_maxminddb
You can enable mod_maxminddb in your VPS or Dedicated Account Control Center (ACC). To enable, follow these steps:
- Log in to the ACC on your VPS or Dedicated account
- In the left sidebar, click Dedicated, then click Apache Options in the drop-down
- In the Apache Module section, check the box next to mod_maxminddb
- Click the Make Changes button
This will enable mod_maxminddb 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_maxminddb 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:MM_COUNTRY_CODE} ^CA$ RewriteRule ^(.*)$ http://www.example.com$1 [R,L]
Replace the CA after MM_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:MM_COUNTRY_CODE} ^(CA|US|MX)$ RewriteRule ^(.*)$ http://www.example.com/na/$1 [R,L]
Replace the country codes CA, US, and MX after MM_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 MM_COUNTRY_CODE CN BlockCountry SetEnvIf MM_COUNTRY_CODE RU BlockCountry #add additional blocked countries here Deny from env=BlockCountry
Replace the country codes CN and RU after MM_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 MM_COUNTRY_CODE CN BlockCountry SetEnvIf MM_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 MM_COUNTRY_CODE US AllowCountry SetEnvIf MM_COUNTRY_CODE CA AllowCountry SetEnvIf MM_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 MM_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 MM_COUNTRY_CODE US AllowCountry SetEnvIf MM_COUNTRY_CODE CA AllowCountry SetEnvIf MM_COUNTRY_CODE MX AllowCountry
Be sure to add the country code to the new lines.