Neiden Norway

How to configure .htaccess redirects based on Geo IP

Recently I had a request, asking how can we set up redirects by country to have country-specific landing pages. This can be done by installing mod_geoip, you can download GeoIP legacy Apache module from GITHUB – compile the source on your environment and configure. Or you could install from a repository.

 For ubuntu you can run the following command:

sudo apt-get install php5-geoip php5-dev libgeoip-dev

Then, run the following command:

sudo pecl install geoip

Once the installation is complete, it will probably tell you that an extension= line cannot be found in your php.ini file. Let’s find your php.ini file and add the required lines:

sudo nano /etc/php5/apache2/php.ini

This will open your php.ini, we have to add “extension=geoip.so” to the [PHP] section:

[PHP]
;AFTER THE PHP SECTION NOT BEFORE
extension=geoip.so

and restart Apache:
sudo service apache2 restart

Now create a .htaccess file. For example, if you want to block clients from Russia and China:

SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
Deny from env=BlockCountry

If you want to redirect based on the country using mod_rewrite in combination with mod_geoip, your .htaccess file could look like this:

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(NL|BE)$
RewriteRule ^(.*)$ http://www.mydomain.com/nl/$1 [L]

For more examples take a look at the website of MaxMind

That’s it – don’t forget to comment below.


Posted

in

,

by

Comments

12 responses to “How to configure .htaccess redirects based on Geo IP”

  1. Tristan Avatar
    Tristan

    Hi Torbjorn, excellent info.

    I tried the following, using your example (masking my domain name for privacy reasons):

    RewriteEngine on
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(NL|BE)$
    RewriteRule ^(.*)$ https://mydomain.com/shop/nl/$1 [L]
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(DE)$
    RewriteRule ^(.*)$ https://mydomain.com/shop/de/$1 [L]RewriteCond %{REQUEST_FILENAME} !-f

    Unfortunately, this gives me an http:500 error. I am probably making a very stupid error somehow, wondering if you could help?

    Kind regards, Tristan

    1. torbjornzetterlund Avatar

      Do you have the rewrite engine enabled in Apache?

  2. Sathish Avatar
    Sathish

    Hi,

    I have followed your article to setup geoip, but i’m using PHP 7.2.7

    This is my code, i have enabled geoip with wordpress, following is my .htaccess code

    I want to redirect my home page for india to https://hostguns.com/in

    For global sit : https://hostguns.com

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(IN)$
    RewriteRule ^(.*)$ https://hostguns.com/in/$1 [L]

    But i this configuration didn’t work well.

    I have tested the htaccess, it is working fine.

    All countries show only https://hostguns.com.

    Can you help me in this ?

    Thanks in advance.

    1. torbjornzetterlund Avatar

      Sorry, been away. Have you been able to resolve.

  3. Johan Avatar
    Johan

    Hi Torbjorn, Nice article. Do you have an article for IP2Location LITE database? It has database with ZIP code and we would like to block by ZIP codes.

    1. torbjornzetterlund Avatar

      Johan,

      No, I don’t have an article on IP2Location LITE database, we have not been able to test how you could block by IP codes. Maybe in your testing, you would like to contribute and article on IP2Location LITE database.

      Torbjorn

  4. Anirudh Avatar
    Anirudh

    How can i redirect a single wordpress page based on his/her location?

    1. Torbjorn Zetterlund Avatar
      Torbjorn Zetterlund

      What do you mean with his/her location?

  5. Mastap Avatar
    Mastap

    Hi
    1-What if we hit the 1000 limit per day?
    Can this solution scale on some paid account? Please point.

    2-What we’re looking for is for those checks to occur ONLY if the user calls the top domain without language sub domain. Any tip?

    Thanks

    1. Torbjorn Zetterlund Avatar
      Torbjorn Zetterlund

      When you hit a limit there will be a cost associated with it, it can scale.

      I would look into Cloudflare – I use Cloudflare in front of my website – there are addons to Cloudflare to do redirects or you can if you have the in-house skills do it with custom workers in javascript in Cloudflare. https://www.cloudflare.com/apps/geo-redirect

  6. kazuma Avatar
    kazuma

    How do I stop this from happening? I want to access pages in my language because I live in a foreign country, but the corporate sites all direct me to my local version of the pages. Things like shopping carts, account logins, and many links become non functional. There used to be a setting in my browser to notify my when the page was trying to redirect me and I could decline it. Is there still a way to do this?

    1. torbjorn Avatar

      You could use a VPN service to connect to the country you want to view a web page at or you could simply if you get a message in your browser to not accept your geo location.

Leave a Reply

Your email address will not be published. Required fields are marked *