Created
March 31, 2018 13:44
-
-
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.
This file contains hidden or 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
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