forked from mozzius/community-handles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
34 lines (29 loc) · 968 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { NextResponse, type NextRequest } from "next/server"
import { getDomain } from "./lib/utils"
export function middleware(request: NextRequest) {
const url = new URL(request.url)
const { domain, subdomain } = getDomain(url.hostname)
if (domain) {
if (subdomain && subdomain !== process.env.LANDING_SUBDOMAIN) {
return NextResponse.rewrite(
new URL(`/${domain}/${subdomain}${url.pathname}${url.search}`, url)
)
} else {
return NextResponse.rewrite(
new URL(`/${domain}${url.pathname}${url.search}`, url)
)
}
}
}
export const config = {
matcher: [
/*
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
* 3. all root files inside /public (e.g. /favicon.ico)
* 4. opengraph images (e.g. /[a-z0-9-_.]/[a-z0-9-_]/opengraph-image)
*/
"/((?!api/|_next/|_static/|[\\w-]+\\.\\w+|[a-zA-Z0-9-_.]+/[a-zA-Z0-9-_]+/opengraph-image).*)",
],
}