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:
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
- Access the main shop page:
shop1.localhost:3000 (should work)
- Access the thank you page:
shop1.localhost:3000/thank-you (should now work)
- 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
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:
Testing Steps
shop1.localhost:3000(should work)shop1.localhost:3000/thank-you(should now work)Expected Behavior
shop1.localhost:3000→ routes to/s/shop1/page.tsxshop1.localhost:3000/thank-you→ routes to/s/shop1/thank-you.tsx