Created
August 26, 2023 05:28
-
-
Save acro5piano/331291a518a3fa63afe706b980da0d76 to your computer and use it in GitHub Desktop.
cloudfront function for static sites
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 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