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(); + } } }