diff --git a/apps/payments/next/app/[locale]/subscriptions/landing/route.ts b/apps/payments/next/app/[locale]/subscriptions/landing/route.ts index 9793a94e73f..dc2def43209 100644 --- a/apps/payments/next/app/[locale]/subscriptions/landing/route.ts +++ b/apps/payments/next/app/[locale]/subscriptions/landing/route.ts @@ -14,6 +14,7 @@ export async function GET( request: NextRequest, { params }: { params: ManageParams } ) { + const currentUrl = request.nextUrl; const requestSearchParams = request.nextUrl.searchParams; const { locale } = params; @@ -53,5 +54,12 @@ export async function GET( console.error(error); } - redirect(redirectUrl ?? redirectToUrl.href); + let finalRedirectUrl = redirectUrl ?? redirectToUrl.href; + // Avoid redirecting back to this same landing URL + if (finalRedirectUrl === currentUrl.href) { + // Prefer the manage URL instead of looping + finalRedirectUrl = redirectToUrl.href; + } + + redirect(finalRedirectUrl); } diff --git a/apps/payments/next/middleware.ts b/apps/payments/next/middleware.ts index 4b351f2e2d9..f31d1ba12fc 100644 --- a/apps/payments/next/middleware.ts +++ b/apps/payments/next/middleware.ts @@ -10,7 +10,9 @@ export function middleware(request: NextRequest) { request.nextUrl.pathname, request.headers.get('accept-language') ); - if (result.redirect) { + + // Redirect only if the pathname has changed to avoid an infinite redirect loop + if (result.redirect && result.pathname !== request.nextUrl.pathname) { return NextResponse.redirect( new URL(`${result.pathname}${request.nextUrl.search}`, request.url) );