Thursday, July 2, 2020

Geo Location Based Redirects with CloudFront and Apache | Redirect Web traffic Based on Country of Origin with CloudFront

This tutorial explain the approach to enable Geo Location based redirects with CloudFront and Apache.

Geo IP based redirection

Geo IP based redirection is the process of automatically redirecting a website visitor by their geolocation.

There are multiple options to enable the location based redirects in Apache, one of the option is using Geo IP database like MaxMind Geo IP database to map users’s IP to their location. Maxmind Geo IP database can be enabled through Apache module.

If you are using any of the CDN e.g CloudFront provides specific headers with request location, CloudFront will detect the user’s country of origin and pass along the county code to origin server in the CloudFront-Viewer-Country header. You can use this information to customize your responses e.g redirecting the users to specific URL based on origin country.

Prerequisites

Website enabled with CloudFront CDN and Apache

CloudFront Configurations

As a first step white list the CloudFront-Viewer-Country header in Cloudfront distribution

Access <<CloudFront Distribution>> →Behaviors → <<Specific Behavior>>

Edit the behavior

Whitelist CloudFront-Viewer-Country header — the header with user’s country of origin will be sent to origin server(Apache) on every request.

CloudFront-Viewer-Country header will have the two letter country code based on the request origin.

Enable Apache Redirect

Let us now enable the required redirect configuration to virtualhost, add the below redirect rules to enable the the country specific redirects

<VirtualHost *:80>

ServerAdmin [email protected]
DocumentRoot "C:\opt\communique\dispatcher\cache"
ServerName test.albinsblog.com
ServerAlias localhost

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^US$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/us/en.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^IT$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/it/it.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^CA
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/ca/en.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^FR$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/fr/fr.html [R=302,L]
<Directory />
Options Indexes FollowSymLinks Includes
# Set includes to process .html files
AddOutputFilter INCLUDES .html
AddOutputFilterByType INCLUDES text/html
AllowOverride None
</Directory>

</VirtualHost>

I am using some VPN tool to initiate the connection from different origin country.

Connected the VPN to Canada

Now the user is redirected to Canada specific URL

The user is redirected to the country specific URL based on the users country of origin, CloudFront will detect the user’s country of origin and pass along the county code to origin server(Apache) in the CloudFront-Viewer-Country header. The Apache server redirect the user to the country specific URL’s based on the country code values in CloudFront-Viewer-Country header.





2 comments:


  1. I am working on a similar rewrites based on geo location have few questions related to this, I hope you can help me

    My requirement is to rewrite the url based on geo location for AU,GB and US is default

    Issue: When I hit the wwww.xyz.com from US I am not seeing US content on the landing page I am seeing the content in this page
    wwwdev.xyz.com/content/abc/en.html similarly if I hit it from GB it's showing the same www.xyz.com/content/abc/en.html content
    but when I hit the complete url www.xyz.com/content/abc/en.html from US or GB it's redirecting to https://www.xyz.com/content/abc/us/en.html and
    https://www.xyz.com/content/abc/au/en.html

    Our requirement we wanted to see the US,AU and GB content on the landing page as well, Is there a way we can achieve that if so Please provide your suggestions

    I applied the below rules in my Apache



    #write bare url to en.html
    RewriteRule ^/$ /content/abc/en.html [PT]

    # Rewrite home.html to en.html
    RewriteRule ^/home.html$ /content/abc/en.html [PT]

    # All links should have the /content/abc removed per Publisher link rewriter, but in case one gets through
    RewriteCond %{REQUEST_URI} !^/content
    RewriteCond %{REQUEST_URI} !^/etc.clientlibs
    RewriteCond %{REQUEST_URI} !^/libs
    # Redirect request to /content/abc
    RewriteRule ^/(.*)$ /content/abc/$1 [PT]

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/content/abc/en.html
    RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^US$
    RewriteRule ^.*$ https://www.xyz.com/content/abc/us/en.html [R=302,L]
    RewriteCond %{REQUEST_URI} ^/content/abc/en.html
    RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^AU$
    RewriteRule ^.*$ https://www.xyz.com/content/abc/au/en.html [R=302,L]
    RewriteCond %{REQUEST_URI} ^/content/abc/en.html
    RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^GB$
    RewriteRule ^.*$ https://www.xyz.com/content/abc/gb/en.html [R=302,L]

    ReplyDelete
  2. You may need to modify the rewrite rule, try something like below

    RewriteCond %{REQUEST_URI} ^/$
    RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^US$
    RewriteRule ^/$ https://www.xyz.com/content/abc/us/en.html [R=302,L]

    ReplyDelete