Skip to content

Instantly share code, notes, and snippets.

@acro5piano
Created August 26, 2023 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acro5piano/331291a518a3fa63afe706b980da0d76 to your computer and use it in GitHub Desktop.
Save acro5piano/331291a518a3fa63afe706b980da0d76 to your computer and use it in GitHub Desktop.
cloudfront function for static sites
function handler(event) {
var request = event.request
var uri = request.uri
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
location: { value: request.uri.replace(/\/$/, '') },
},
}
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '.html'
}
return request
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment