Skip to content

Instantly share code, notes, and snippets.

@prenagha
Created September 2, 2019 15:44
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 prenagha/6b9a335d6ef0aab3de3ba89faf318146 to your computer and use it in GitHub Desktop.
Save prenagha/6b9a335d6ef0aab3de3ba89faf318146 to your computer and use it in GitHub Desktop.
Lambda@Edge to redirect to default HTML page
'use strict';
const path = require('path');
/**
* Redirects URI to default document.
* Any URI without an extension is assumed to be
* a directory reference and will have '/index.html'
* appended to the URI.
* Useful as Lambda@Edge on CloudFront Origin Request event
*/
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const uri = request.uri;
const parsedPath = path.parse(uri);
if (parsedPath.ext === '') {
let newUri = path.join(parsedPath.dir, parsedPath.base, 'index.html');
// Update to the URI that includes the index page
request.uri = newUri;
}
callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment