Skip to content

Instantly share code, notes, and snippets.

@EnriqueTejeda
Last active August 3, 2022 17:20
Show Gist options
  • Save EnriqueTejeda/3c80f8b5c9bb08111a53e25711001395 to your computer and use it in GitHub Desktop.
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
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