Skip to content

Commit 2510166

Browse files
authored
Merge pull request #21453 from dvdksn/handle-double-trailing-slash
lambda: collapse redundant slashes in requests
2 parents 24d55b8 + 209d00f commit 2510166

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

hack/releaser/cloudfront-lambda-redirects.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
exports.handler = (event, context, callback) => {
44
//console.log("event", JSON.stringify(event));
55
const request = event.Records[0].cf.request;
6-
const requestUrl = request.uri.replace(/\/$/, "")
6+
// Trim trailing slash and collapse redundant slashes
7+
const normalizedUri = request.uri.replace(/\/$/, "").replaceAll(/\/{2,}/g, "/")
78

89
const redirects = JSON.parse(`{{.RedirectsJSON}}`);
910
for (let key in redirects) {
1011
const redirectTarget = key.replace(/\/$/, "")
11-
if (redirectTarget !== requestUrl) {
12+
if (redirectTarget !== normalizedUri) {
1213
continue;
1314
}
14-
//console.log(`redirect: ${requestUrl} to ${redirects[key]}`);
15+
//console.log(`redirect: ${normalizedUri} to ${redirects[key]}`);
1516
const response = {
1617
status: '301',
1718
statusDescription: 'Moved Permanently',
@@ -52,5 +53,6 @@ exports.handler = (event, context, callback) => {
5253
return
5354
}
5455

56+
request.uri = normalizedUri
5557
callback(null, request);
5658
};

0 commit comments

Comments
 (0)