Last active
May 20, 2022 10:34
-
-
Save ArthurKnoep/8a402469e8389c394414089fbdc19256 to your computer and use it in GitHub Desktop.
Redirecting Traffic Based on Simple Conditions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function handler(event) { | |
var request = event.request; | |
var supportedCountries = ['de', 'it', 'fr']; | |
if (request.uri.substr(3, 1) != '/') { | |
var headers = request.headers; | |
var nextUri; | |
if (headers['cloudfront-viewer-country']) { | |
var countryCode = headers['cloudfront-viewer-country'].value.toLowerCase(); | |
if (supportedCountries.includes(countryCode)) { | |
nextUri = '/' + countryCode + request.uri; | |
} | |
} | |
if (!nextUri) { | |
nextUri = '/en' + request.uri; | |
} | |
return { | |
statusCode: 302, | |
statusDescription: 'Found', | |
headers: { | |
location: { value: newUri } | |
} | |
} | |
} | |
return request; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment