diff --git a/src/redirects.ts b/src/redirects.ts --- a/src/redirects.ts +++ b/src/redirects.ts @@ -101,20 +101,14 @@ { source: "/steam", destination: "/disabled-redirect" }, ]; -export function handleRedirect(url: URL) { +export function handleRedirect(pathname: string) { for (const redirect of redirectList) { - if ( - typeof redirect.source === "string" && - url.pathname === redirect.source - ) { + if (typeof redirect.source === "string" && pathname === redirect.source) { return redirect.destination; } - if ( - redirect.source instanceof RegExp && - redirect.source.test(url.pathname) - ) { - const match = redirect.source.exec(url.pathname); + if (redirect.source instanceof RegExp && redirect.source.test(pathname)) { + const match = redirect.source.exec(pathname); if (match) { return redirect.destination.replace("$1", match[1] || ""); } diff --git a/src/pages/[...route].astro b/src/pages/[...route].astro --- a/src/pages/[...route].astro +++ b/src/pages/[...route].astro @@ -3,21 +3,24 @@ export const prerender = false; import { + defaultLocale, + localeCodes, removeTrailingSlash, useTranslatedPaths, - localeCodes, - defaultLocale, } from "@i18n"; import { handleRedirect } from "../redirects"; +const [_, langParam, ...restUrl] = Astro.url.pathname.split("/"); +const noLangPathname = localeCodes.includes(langParam) + ? `/${restUrl.join("/")}` + : Astro.url.pathname; + // check for redirections first -const redirectedUrl = handleRedirect(Astro.url); +const redirectedUrl = handleRedirect(noLangPathname); if (redirectedUrl) { - return Astro.redirect(redirectedUrl); + return Astro.redirect(removeTrailingSlash(redirectedUrl)); } - -const [_, langParam] = Astro.url.pathname.split("/"); // if there's no lang parameter, detect language and redirect if (!langParam || !localeCodes.includes(langParam)) {