diff --git a/src/middleware.ts b/src/middleware.ts index 2293ab8..2122b5d 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -16,12 +16,19 @@ export function middleware(request: NextRequest) { ); if (hasLocale || PUBLIC_FILE.test(pathname)) return NextResponse.next(); - // Relative Location keeps the public host; NextURL.origin is the PM2 bind address. - const location = `/${defaultLocale}${pathname === "/" ? "" : pathname}${request.nextUrl.search}`; - return new NextResponse(null, { - status: 307, - headers: { Location: location }, - }); + // nextUrl.origin is the PM2 bind address (localhost:3024). Use the public Host. + const host = + request.headers.get("x-forwarded-host")?.split(",")[0]?.trim() || + request.headers.get("host"); + const proto = + request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim() || "https"; + const origin = host ? `${proto}://${host}` : request.nextUrl.origin; + const dest = new URL( + `/${defaultLocale}${pathname === "/" ? "" : pathname}`, + origin, + ); + dest.search = request.nextUrl.search; + return NextResponse.redirect(dest); } export const config = {