From 4be6eada62e2dec845e37f6e014543024b29d987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Tue, 26 Mar 2024 22:30:47 +0000 Subject: [PATCH] Fix redirect to right page for content redirects (#2241) --- src/middleware.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index a3ba008fa..55470c74f 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -570,19 +570,25 @@ async function lookupSpaceByAPI( if ('redirect' in data) { if (alternative.primary) { - // Append the path to the redirect URL if it's a VA redirect + // Append the path to the redirect URL // because we might have matched a shorter path and the redirect is relative to it if (alternative.extraPath) { - const redirect = new URL(data.redirect); - if (redirect.searchParams.has('location')) { - redirect.searchParams.set( - 'location', - joinPath( - redirect.searchParams.get('location') ?? '', - alternative.extraPath, - ), - ); + if (data.target === 'content') { + const redirect = new URL(data.redirect); + redirect.pathname = joinPath(redirect.pathname, alternative.extraPath); data.redirect = redirect.toString(); + } else { + const redirect = new URL(data.redirect); + if (redirect.searchParams.has('location')) { + redirect.searchParams.set( + 'location', + joinPath( + redirect.searchParams.get('location') ?? '', + alternative.extraPath, + ), + ); + data.redirect = redirect.toString(); + } } }