Redirect Web traffic Based on Country of Origin with CloudFront
By: Albin Issac
Tags: Cloudfront, Programming, Software Development, Geo Based Redirect, Apache

Geo-Location Based Redirects with CloudFront and Apache | Redirect Web traffic Based on Country of Origin with CloudFront Albin Issac Follow Jul 2 · 3 min read

This tutorial explains 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 a Geo IP database like the MaxMind Geo IP database to map users’ IP to their location. Maxmind Geo IP database can be enabled through the 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 the origin server in the CloudFront-Viewer-Country header. You can use this information to customize your responses e.g redirecting the users to specific URLs based on origin country.

Prerequisites

The 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 the user’s country of origin will be sent to the origin server(Apache) on every request.

The 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 country-specific redirects

<VirtualHost *:80>



ServerAdmin webmaster@dummy-host.example.com

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 tools to initiate the connection from different origin countries.

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 the origin server(Apache) in the CloudFront-Viewer-Country header. The Apache server redirects the user to the country-specific URLs based on the country code values in the CloudFront-Viewer-Country header.