Here we will use a geolocation DB combined with a Litespeed webserver to redirect visitors on our site to a different version or language of the site based on their location.
In this example, we are using Centos 7 and have the server up and running with a Litespeed web server.
The first thing we going to install GeoIP packages:
#yum install -y geoip geoipupdate
Next, we will need is a Geolocation DB, our DB of choice is MaxMind GeoLite2, due to licensing changes we will need to sign up for the service at https://www.maxmind.com/en/geolite2/signup
After we successfully opened an account and logged in we will need to generate a license at Services > My License Key > Generating new license key
CentOS 7 uses geoipupdate 2.5 so we will choose to generate the key for versions older than 3.1.1 and after confirming click on Download Config and save this file. Now that we have the license, let’s edit “/etc/GeoIP.conf” and replace the content with what we just got from MaxMind.
Geoipupdate is setup now lets run the update:
#/usr/bin/geoipupdate
this will download the databases to /usr/share/GeoIP
It’s recommended to create a cron for geoipupdate once a month to keep it up to date.
Now that we have geoip packages and DB let’s configure LiteSpeed,
Login to Litespeed admin panel go to Server > General Settings > Edit and Enable GeoLocation Lookup, and in Server > MaxMind GeoIP DB > add
DB location we downloaded earlier: /usr/share/GeoIP/GeoLite2-Country.mmdb
db name (in this case): DB Name
Save & Graceful Restart.
All should be set now! We can go to our site .htaccess file or Rewrite Rules in Litespeed admin panel and add the required rules for our use case.
Here are a few examples:
#Redirect visitor from Poland to the polish version of the site RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(PL)$ RewriteCond %{REQUEST_URI} !^/pl RewriteRule ^(.*)$ /pl [R=301,L] #Redirect visitors from Poland to other domain. RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(PL)$ RewriteRule ^(.*)$ https://domain.pl [R=301,L]
”PL” is an example of iso country code of Poland, we can replace or add other country codes.
GEOIP_COUNTRY_CODE is one of the default environment variable Litespeed set:
"GEOIP_COUNTRY_CODE", "/country/iso_code" "GEOIP_CONTINENT_CODE", "/continent/code" "GEOIP_REGION", "/subdivisions/0/iso_code" "GEOIP_METRO_CODE", "/location/metro_code" "GEOIP_LATITUDE", "/location/latitude" "GEOIP_LONGITUDE", "/location/longitude" "GEOIP_POSTAL_CODE", "/postal/code" "GEOIP_CITY", "/city/names/en"
under Server > MaxMind GeoIP DB we can set our own variable as needed.
Tags: litespeed
Leave a Reply