#462·platforms

Query Parameters Lost After Middleware Rewrite for Subdomain Routing in Next.js App Router

Author: VibeesarmaCreated Jun 20, 2025Updated Jun 20, 2025

When accessing a subdomain route such as http://domain.localhost:3001/thank-you?card_id=order_XXXXXXX the middleware rewrites the path to /s/vibees-sarma/thank-you, but the query parameters (e.g., card_id) are not preserved in the rewritten request. As a result, searchParams in the corresponding page component is always empty.

Steps to Reproduce:

Visit a subdomain route with query parameters, e.g. http://domain.localhost:3001/thank-you?card_id=order_XXXXXXXX The middleware rewrites the path to /s/vibees-sarma/thank-you. In the page component, props.searchParams is {}. Expected Behavior: The query parameters should be preserved after the rewrite, so props.searchParams contains the expected values.

Actual Behavior: props.searchParams is always empty after the rewrite.

Relevant Code:

typescript
// middleware.ts excerpt
if (subdomain) {
  // For other paths on subdomain (like /thank-you)
  const url = new URL(request.url);
  url.pathname = `/s/${subdomain}${pathname}`;
  return NextResponse.rewrite(url);
}

Suggested Fix: Ensure that the rewritten URL preserves the original query parameters by constructing the new URL using the original request.url and only changing the pathname. This way, the query string is retained and available in the page component.