Last active
August 3, 2022 17:20
-
-
Save EnriqueTejeda/3c80f8b5c9bb08111a53e25711001395 to your computer and use it in GitHub Desktop.
CloudFront Function to rewrite uri in s3 backend (leave only the last path) & add index.html for static websites
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; | |
// Example: | |
// Input: /app1/assets/test.jpg | |
// Output: /assets/test.jpg | |
request.uri = request.uri.replace(/^\/[^/]*\//, "/"); | |
// Check whether the URI is missing a file name. | |
if (request.uri.endsWith('/')) { | |
request.uri += 'index.html'; | |
} | |
// Check whether the URI is missing a file extension. | |
else if (!request.uri.includes('.')) { | |
request.uri += '/index.html'; | |
} | |
return request; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment