Redirect URL with the Apache Web Server using IP2Location

Intro

Apache is a popular open-source web server being used on both the Linux and Windows environments. In this article, we will demonstrate how to enable geolocation redirection in Apache via the IP2Location Apache Module for the Debian Linux environment.

Pre-requisites

We will assume that you have a functioning Apache 2.4 web server. We will not cover the installation of Apache 2.4 as there are many guides on the web for that. You will need to install the apache2-dev package if you don’t have it installed.

sudo apt-get install apache2-dev

 

The last pre-requisite is the IP2Location BIN database file which you can get from:

https://www.ip2location.com/database/ip2location (commercial)

or

https://lite.ip2location.com/ip2location-lite (free LITE version, less accurate)

Download and decompress the BIN file into a folder called ip2location.

IP2Location LITE database

Installation

First of all, we need to get the IP2Location C library via the below link:

https://github.com/chrislim2888/IP2Location-C-Library/archive/master.zip

Download the above and decompress in a folder called ip2location.

Navigate to the IP2Location-C-Library-master sub-folder.

cd IP2Location-C-Library-master

Now, compile the IP2Location C library by running the below commands.

sudo autoreconf -i -v --force
sudo ./configure
sudo make
sudo make install

Next, get the IP2Location Apache module via the below link:

https://github.com/ip2location/ip2location-apache/archive/master.zip

Download the above and decompress into the ip2location folder from above.

In your ip2location folder, you should now have 2 sub-folders called ip2location-apache-master and IP2Location-C-Library-master. Navigate to the ip2location-apache-master folder.

cd ../ip2location-apache-master

Now, we will compile both the IP2Location C library and the IP2Location Apache module.

sudo apxs2 -i -a -L /usr/local/lib/ -I ../IP2Location-C-Library-master/libIP2Location/ -l IP2Location -c mod_ip2location.c

Configuration

Add following lines into the /etc/apache2/apache2.conf file. Modify the IP2LocationDBFile parameter for your own folder and BIN filename.

<IfModule mod_ip2location.c>
  IP2LocationEnable On
  IP2LocationDetectProxy Off
  IP2LocationSetmode ENV
  IP2LocationDBFile "/home/admin/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN"
</IfModule>

Turning on the rewrite module

The redirection is performed by the Apache rewrite module so if you haven’t enabled it, you can do so using the command below.

sudo a2enmod rewrite

Restart Apache for the above settings to take effect

Run the following command to restart.

sudo systemctl restart apache2

Testing redirection

In your website .htaccess file, paste something like the following:

RewriteEngine On
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^SE$
RewriteRule ^(.*)$ https://www.google.se/ [L]
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^MY$
RewriteRule ^(.*)$ https://www.google.com.my/ [L]

The above checks for the country of the IP address. If the country is Sweden (SE) then the visitor is redirected to the Swedish Google page. If the country is Malaysia (MY) then the visitor will be redirected to the Malaysian Google page.

Was this article helpful?

Related Articles