#460·platforms

For other paths on subdomain

Author: VibeesarmaCreated Jun 16, 2025Updated Jun 16, 2025

Root Cause

The middleware's routing logic only handles the root path for subdomains but doesn't properly rewrite other paths to include the shop slug.

Solution

Update the middleware to properly handle all paths under subdomains:

typescript
export async function middleware(request: NextRequest) {
  const { pathname } = request.nextUrl;
  const subdomain = extractSubdomain(request);

  if (subdomain) {
    // Block access to admin page from subdomains
    if (pathname.startsWith("/admin")) {
      return NextResponse.redirect(new URL("/", request.url));
    }

    // For the root path on a subdomain
    if (pathname === "/") {
      return NextResponse.rewrite(new URL(`/s/${subdomain}`, request.url));
    }

    // For other paths on subdomain (like /thank-you)
    return NextResponse.rewrite(new URL(`/s/${subdomain}${pathname}`, request.url));
  }

  return NextResponse.next();
}

Testing Steps

  1. Access the main shop page: shop1.localhost:3000 (should work)
  2. Access the thank you page: shop1.localhost:3000/thank-you (should now work)
  3. Verify other routes under the subdomain work as expected

Expected Behavior

  • shop1.localhost:3000 → routes to /s/shop1/page.tsx
  • shop1.localhost:3000/thank-you → routes to /s/shop1/thank-you.tsx