yogesh vaidya
yogesh vaidya

Reputation: 117

Redirection of urls based on geo location on apache 2 server using mod_geoip2

My use case is of showing different content based on the user country. I do have separate HTML5 pages for different countries in separate directories. I want to internally redirect the user to different directories based on his location. I am not able to follow the documentation about setting up mod_geoip2 with GeoLite2 databases on rhel apache2 server.

I tried using mod_geoip apache package but I found that the GeoLite database is no longer updated by maxmind. Hence, I need to use GeoLite2 databases and mod_geoip2 packages.

I am used to with redirection rules, so I can do the code stuff in the httpd.conf file

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$
RewriteRule ^(.*)\.html$ /path/to/directory$ [PT]

Upvotes: 0

Views: 1629

Answers (1)

Vlam
Vlam

Reputation: 1798

I believe using the IP2Location Apache module is a lot easier based on the guide in https://blog.ip2location.com/knowledge-base/redirect-url-with-the-apache-web-server-using-ip2location/

Basically, you will need 3 things.

Download those 3 things and unzip them into a folder called ip2location.

Navigate to the IP2Location C Libary folder and compile it with the below commands:

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

Now navigate to the IP2Location Apache Module folder and compile it with the below commands:

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

In your /etc/apache2/apache2.conf file, you can paste the below after editing the IP2LocationDBFile to use your own path to the BIN file:

<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>

Restart Apache for the changes to take effect.

sudo systemctl restart apache2

Now, you can add your redirection logic in your .htaccess file. Example usage below:

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]

Upvotes: 0

Related Questions