Quantcast
Channel: Infrastucture – Adobe Experience Manager Podcast
Viewing all articles
Browse latest Browse all 36

How to Configure AEM Dispatcher to Redirect Based on Geolocation

$
0
0

There are many reasons you might want to take action on your Adobe Experience Manager website based on the physical location from which your users are coming. For example, you might have multiple language versions of your site and want to provide users from France with the French version of the site by default, or perhaps you want to block everyone from a specific region from even being able to reach your site. Whatever the case may be, mod_geoip is here to help.

We’ll assume you’ve already installed Apache. mod_geoip2 from MaxMind allows you to perform traffic shaping based on the geographical location of the IP address from which a client request originates. Axis41 generally uses a RedHat-like Linux distribution, where this module is available in the “EPEL” repository under the name mod_geoip.

If you haven’t set up the EPEL repository, you can enable it with a command such as this:

sudo yum-config-manager --enable epel

Once the EPEL repo is enabled, you can instruct your package manager to download and install mod_geoip, as well as the GeoIP data and some related libraries like so:

sudo yum install GeoIP GeoIP-devel zlib-devel mod_geoip

MaxMind, the company behind GeoIP, regularly updates its database files; we recommend creating a cron job to update to the latest version of these files on a regular basis.  Here we present a series of commands to create a bash script to perform these steps, which will most likely require root privileges. Our example is based on the free “GeoLite” data files; if you have a license from MaxMind to the commercial version, you can replace the URLs as appropriate:

#Create shell script to update database files
cd /usr/share/GeoIP/
#On other distros this could be in /var/lib/GeoIP/
#Do not quote the heredoc terminator on RHEL environments
cat > geoipupdate.sh <<'EOT'
#Download Maxmind GeoIP databases
GEOIP_PATH="/usr/share/GeoIP/"
CUR_DATE=`date +%m-%d-%y`

cd ${GEOIP_PATH}

wget -q  http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

if [[ ! -s GeoIP.dat.gz ]] || [[ ! -s GeoLiteCity.dat.gz ]]; then
        echo "Zero Byte File Detected, exiting..."
        exit
fi

for i in GeoIP.dat GeoLiteCity.dat; do
        mv -f ${i} ${i}.${CUR_DATE}
        gunzip ${i}.gz
done

EOT

Change the permissions on your script so it has the correct rights and add it to your crontab:

chmod 750 geoipupdate.sh
#Edit your crontab to run this script every 3rd day of the month:
(crontab -l ; echo "0 0 3 * * /usr/share/GeoIP/geoipupdate.sh")| crontab -

Edit the configuration file /etc/httpd/conf.d/geoip.conf and change the path of the database. Use your system’s facility for enabling an Apache module to activate the geoip module; in our case, we will be adding the “LoadModule” line directly to the geoip.conf file (see below).  (GeoLiteCity.dat also returns the City Names and the geographic locations):

LoadModule geoip_module modules/mod_geoip.so

<IfModule mod_geoip.c>
  GeoIPEnable On
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>

Now create or edit your vhosts file. For example, if you want to block clients from Russia and China, you might use the following example:

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 vhosts file could look like this:

RewriteEngine on
#Redirects based on country
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^FR$
RewriteCond %{REQUEST_URI} !/content/yoursite/fr
RewriteRule ^(.*)$ /content/yoursite/fr$1 [R,L]

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^DE$
RewriteCond %{REQUEST_URI} !/content/yoursite/de
RewriteRule ^(.*)$ /content/yoursite/fr$1 [R,L]

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UK$
RewriteCond %{REQUEST_URI} !/content/yoursite/uk
RewriteRule ^(.*)$ /content/yoursite/fr$1 [R,L]

Restart Apache:

service httpd restart

For more on what mod_geoip2 can do for your Adobe Experience Manager Website, visit the MaxMind website, where you can find more information on how to serve up meaningful content to users based on their preferences, location, and other data. And if you need help configuring or fine tuning your Adobe Experience Manager infrastructure (Dispatcher, Publish, or Author) to your needs then please contact us or send us an email info@aempodcast.com.


Viewing all articles
Browse latest Browse all 36

Trending Articles