| 1 | import { NextRequest, NextResponse } from "next/server"; |
| 2 | |
| 3 | export function middleware(request: NextRequest) { |
| 4 | const host = request.headers.get("host") ?? ""; |
| 5 | const hostname = host.split(":")[0]; // strip port |
| 6 | |
| 7 | if (hostname.startsWith("canopy.")) { |
| 8 | const url = request.nextUrl.clone(); |
| 9 | url.pathname = `/canopy${url.pathname}`; |
| 10 | return NextResponse.rewrite(url); |
| 11 | } |
| 12 | if (hostname.startsWith("ring.")) { |
| 13 | const url = request.nextUrl.clone(); |
| 14 | url.pathname = `/ring${url.pathname}`; |
| 15 | return NextResponse.rewrite(url); |
| 16 | } |
| 17 | if (hostname.startsWith("collab.")) { |
| 18 | const url = request.nextUrl.clone(); |
| 19 | url.pathname = `/collab${url.pathname}`; |
| 20 | return NextResponse.rewrite(url); |
| 21 | } |
| 22 | return NextResponse.next(); |
| 23 | } |
| 24 | |
| 25 | export const config = { |
| 26 | matcher: [ |
| 27 | // Match all paths except Next.js internals and static files |
| 28 | "/((?!_next/static|_next/image|favicon\\.svg|canopy-favicon\\.svg|ring-favicon\\.svg|collab-favicon\\.svg|api/).*)", |
| 29 | ], |
| 30 | }; |
| 31 | |