forked from whisperrnote/whisperrnote
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
46 lines (39 loc) · 1.42 KB
/
middleware.ts
File metadata and controls
46 lines (39 loc) · 1.42 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const hostname = request.headers.get('host') || 'kylrix.space';
const { pathname } = request.nextUrl;
// Root domain: no rewrites
if (hostname === 'kylrix.space' || hostname === 'www.kylrix.space') {
return NextResponse.next();
}
// App subdomain: only rewrite root to /notes to avoid loops
if (hostname === 'note.kylrix.space') {
if (pathname === '/' || pathname === '') {
return NextResponse.rewrite(new URL('/notes', request.url));
}
// Do not prefix all paths with /notes to avoid recursive rewrites
return NextResponse.next();
}
// Auth subdomain: only rewrite root to /login (guard against loops)
if (hostname === 'accounts.kylrix.space') {
if (pathname === '/' || pathname === '') {
return NextResponse.rewrite(new URL('/login', request.url));
}
return NextResponse.next();
}
// Send subdomain: only rewrite root to /send (guard against loops)
if (hostname === 'send.kylrix.space') {
if (pathname === '/' || pathname === '') {
return NextResponse.rewrite(new URL('/send', request.url));
}
return NextResponse.next();
}
return NextResponse.next();
}
export const config = {
matcher: [
// Match all paths except Next internals and API
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};