Created
April 10, 2022 16:04
-
-
Save tsungtwu/b196c166d33bfe9e8076159e94858ba1 to your computer and use it in GitHub Desktop.
Use AWS CloudFront Functions for URI Rewrites
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 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; | |
} |
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; | |
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