Redirect landing page based on country or regions using AWS

Consider a website have different landing pages for different regions

 mywebsite.com/eu for Europe region

 mywebsite.com/na for North and South America

 mywebsite.com/as for Asia and Australia

mywebsite.com/af for all other world

This can be achieved by AWS country header from CloudFront. We have to add ‘cloudfront-viewer-country’ header on CloudFront. We will get a two letter country code on each request.

Based on the country code from request header, we can redirect to respective page.

How to do it using PHP?

If you are using PHP with composer, we have a great choice to get the country and region.

composer require ishdidev/country

$countryList = $country->getCountries();

$countryContinent = $country->getData('IN', Country::COUNTRY_CONTINENT);

You will get 2 letter continent code from the above call. Based on that you can redirect to respective pages.