Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Created July 1, 2020 06:20
Show Gist options
  • Save neilkuan/5e1942a1faf4e58f984dd12a0cf7c27c to your computer and use it in GitHub Desktop.
Save neilkuan/5e1942a1faf4e58f984dd12a0cf7c27c to your computer and use it in GitHub Desktop.
exports.handler = (event, context, callback) => {
// Extract the request from the CloudFront event that is sent to Lambda@Edge
var request = event.Records[0].cf.request;
var newuri = ''
// Extract the URI from the request
var olduri = request.uri;
if (olduri == '/'){
newuri = olduri+ 'index.html';
}else{
if (olduri.endsWith('/')) {
newuri = olduri.replace(/\/$/, '\/index.html');
} else {
newuri = olduri + '/index.html';
}
}
// Match any '/' that occurs at the end of a URI. Replace it with a default index
// Log the URI as received by CloudFront and the new URI to be used to fetch from origin
console.log("Old URI: " + olduri);
console.log("New URI: " + newuri);
// Replace the received URI with the URI that includes the index page
request.uri = newuri;
// Return to CloudFront
return callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment