Created
September 2, 2019 15:44
-
-
Save prenagha/6b9a335d6ef0aab3de3ba89faf318146 to your computer and use it in GitHub Desktop.
Lambda@Edge to redirect to default HTML page
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
'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