Skip to content

Instantly share code, notes, and snippets.

@furkan3ayraktar
Created March 31, 2018 13:44
Show Gist options
  • Save furkan3ayraktar/ed818932f0b122188ce25824f97d1f6c to your computer and use it in GitHub Desktop.
Save furkan3ayraktar/ed818932f0b122188ce25824f97d1f6c to your computer and use it in GitHub Desktop.
Redirect requests to index.html within same folder from CloudFront with Labda@Edge.
const path = require('path');
exports.handler = (event, context, callback) => {
const { request } = event.Records[0].cf;
console.log('Request URI: ', request.uri);
const parsedPath = path.parse(request.uri);
let newUri;
console.log('Parsed Path: ', parsedPath);
if (parsedPath.ext === '') {
newUri = path.join(parsedPath.dir, parsedPath.base, 'index.html');
} else {
newUri = request.uri;
}
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