-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (26 loc) · 1010 Bytes
/
middleware.ts
File metadata and controls
31 lines (26 loc) · 1010 Bytes
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
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl
const host = request.headers.get('host') || ''
// Redirect .com to .org (permanent 301)
if (host.includes('buildingopen.com')) {
return NextResponse.redirect(
`https://buildingopen.org${pathname}${search}`,
{ status: 301 }
)
}
// wrapped.buildingopen.org: rewrite paths to /wrapped prefix
if (host.startsWith('wrapped.')) {
if (pathname === '/') {
return NextResponse.rewrite(new URL('/wrapped', request.url))
}
if (!pathname.startsWith('/wrapped') && !pathname.startsWith('/api/') && !pathname.startsWith('/_next/')) {
return NextResponse.rewrite(new URL(`/wrapped${pathname}`, request.url))
}
return NextResponse.next()
}
return NextResponse.next()
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon\\.ico|favicon\\.png|logo\\.svg|og-image\\.png).*)'],
}