Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use AWS CloudFront Functions for URI Rewrites
function handler(event) {
var request = event.request;
var rewrites = [
['/summer21','/recipies?year=2021&season=summer'],
['/recipies/homemade-mouse-and-cheese/', '/recipies/homemade-mac-and-cheese/'],
['/recipies/camping/grilling', '/recipies?activities=camping&with=grill']
]
for (var arrayIndex in rewrites){
if (request.uri == rewrites[arrayIndex][0]) {
var newUri = rewrites[arrayIndex][1];
var response = {
statusCode: 301,
statusDescription: 'Permanently moved',
headers: {
"location": { "value": newUri }
}
}
return response;
}
}
return request;
}
function handler(event) {
var request = event.request;
if (request.headers.host.value === 'example.com' || request.headers.host.value === 'www.example.com') {
var response = {
statusCode: 301,
statusDescription: 'Permanently moved',
headers: {
"location": { "value": 'https://www.otherexample.com/pathroute' }
}
}
return response;
}
return request;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment