Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/redirects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# provide a short, permanent link to refer to a topic in the documentation.
# For example, the docker CLI can output https://docs.docker.com/go/some-topic
# in its help output, which can be redirected to elsewhere in the documentation.
"/learn/":
- /learn
"/security/access-tokens/":
- /go/access-tokens/
"/reference/api/engine/#deprecated-api-versions":
Expand Down
7 changes: 6 additions & 1 deletion hack/releaser/cloudfront-lambda-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ exports.handler = (event, context, callback) => {
const redirects = JSON.parse(`{{.RedirectsJSON}}`);
for (let key in redirects) {
const redirectTarget = key.replace(/\/$/, "");
if (redirectTarget !== requestUrl) {
const exactMatch = key === request.uri;
Comment thread
dvdksn marked this conversation as resolved.
// Preserve slash-insensitive matching without redirecting a canonical
// slash-terminated destination back to itself.
const normalizedMatch =
redirectTarget === requestUrl && redirects[key] !== request.uri;
if (!exactMatch && !normalizedMatch) {
continue;
}
//console.log(`redirect: ${requestUrl} to ${redirects[key]}`);
Expand Down
11 changes: 11 additions & 0 deletions hack/releaser/cloudfront-lambda-redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Module = require("node:module");
const SRC = path.join(__dirname, "cloudfront-lambda-redirects.js");

const REDIRECTS = {
"/learn": "/learn/",
"/old/page/": "/new/page/",
"/target-with-query/": "/dest/?ref=docs",
"/target-with-fragment/": "/dest/?ref=docs#install",
Expand Down Expand Up @@ -85,6 +86,16 @@ test("exact redirect without a query string is unchanged", async () => {
assert.equal(locationOf(result), "/new/page/");
});

test("bare path redirects without redirecting the canonical target to itself", async () => {
const { result: bareResult } = await invoke({ uri: "/learn" });
assert.equal(bareResult.status, "301");
assert.equal(locationOf(bareResult), "/learn/");

const { result: canonicalResult, request } = await invoke({ uri: "/learn/" });
assert.equal(canonicalResult, request);
assert.equal(request.uri, "/learn/index.html");
});

test("exact redirect appends with & when target already has a query string", async () => {
const { result } = await invoke({
uri: "/target-with-query/",
Expand Down