Skip to content

Instantly share code, notes, and snippets.

@iDVB
Created February 20, 2018 19:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iDVB/64d9f859345e7806accf85c5ad48ed70 to your computer and use it in GitHub Desktop.
Save iDVB/64d9f859345e7806accf85c5ad48ed70 to your computer and use it in GitHub Desktop.
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