Skip to content

Instantly share code, notes, and snippets.

@ArthurKnoep
Last active May 20, 2022 10:34
Show Gist options
  • Save ArthurKnoep/8a402469e8389c394414089fbdc19256 to your computer and use it in GitHub Desktop.
Save ArthurKnoep/8a402469e8389c394414089fbdc19256 to your computer and use it in GitHub Desktop.
Redirecting Traffic Based on Simple Conditions
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