Skip to content

Replaces Lambda@Edge with CloudFront Function #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 28, 2022
Prev Previous commit
Next Next commit
Adds function for permanent redirect
  • Loading branch information
nvnivs committed Apr 28, 2022
commit 7b0e92b10011d13319a4a7b5b037881a2fc9a292
20 changes: 12 additions & 8 deletions modules/cloudfront/function_rewrite/index.js.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ function handler(event) {
var uri = request.uri;

try {
%{ for match, destination in REDIRECTS }
%{ for match, target in REDIRECTS }
if (/${match}/.test(uri)) {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
'location': { value: uri.replace(/${match}/, '${destination}') }
}
};
return permanentRedirect(/${match}/, '${target}');
}
%{ endfor ~}

Expand All @@ -27,3 +21,13 @@ function handler(event) {

return request;
}

function permanentRedirect(match, target) {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
'location': { value: uri.replace(match, target) }
}
};
}