Skip to content

Instantly share code, notes, and snippets.

@mucahitnezir
Created September 21, 2020 12:41
Show Gist options
  • Save mucahitnezir/df229e54a839295fde1a22c1dfa37d84 to your computer and use it in GitHub Desktop.
Save mucahitnezir/df229e54a839295fde1a22c1dfa37d84 to your computer and use it in GitHub Desktop.
Lambda function for url rewriting: lambda@edge function
const appendToDirs = 'index.html';
const regexSuffixless = /\/[^/.]+$/; // e.g. "/some/page" but not "/", "/some/" or "/some.jpg"
const regexTrailingSlash = /.+\/$/; // e.g. "/some/" or "/some/page/" but not root "/"
exports.handler = (event, context, callback) => {
const { request } = event.Records[0].cf;
const { uri } = request;
// Append ".html" to origin request
if (uri.match(regexSuffixless)) {
request.uri = uri + '/' + appendToDirs;
callback(null, request);
return;
}
// Append "index.html" to origin request
if (uri.match(regexTrailingSlash)) {
request.uri = uri + appendToDirs;
callback(null, request);
return;
}
// If nothing matches, return request unchanged
callback(null, request);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment