Skip to content

Instantly share code, notes, and snippets.

@tsungtwu
Created April 10, 2022 16:04
Show Gist options
  • Save tsungtwu/b196c166d33bfe9e8076159e94858ba1 to your computer and use it in GitHub Desktop.
Save tsungtwu/b196c166d33bfe9e8076159e94858ba1 to your computer and use it in GitHub Desktop.
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