Created
February 20, 2018 19:16
-
-
Save iDVB/64d9f859345e7806accf85c5ad48ed70 to your computer and use it in GitHub Desktop.
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'); | |
const { STATUS_CODES } = require('http'); | |
exports.rewriteHandler = (evt, ctx, cb) => { | |
const { request } = evt.Records[0].cf; | |
const htmlExtRegex = /(.*)\.html?$/; | |
if (htmlExtRegex.test(request.uri)) { | |
const uri = request.uri.replace(htmlExtRegex, '$1'); | |
return cb(null, redirect(uri)); | |
} | |
if (!path.extname(request.uri)) { | |
request.uri = '/index.html'; | |
} | |
cb(null, request); | |
}; | |
function redirect(to) { | |
return { | |
status: '301', | |
statusDescription: STATUS_CODES['301'], | |
headers: { | |
location: [{ key: 'Location', value: to }], | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment