-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
56 lines (48 loc) · 1.42 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { NextRequest, NextResponse } from 'next/server';
export function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
const host = req.headers.get('host') || '';
// Split the host into its subdomains
const domains = host.split('.');
// Initialize subdomain
let subdomain = '';
// Determine if the request is from localhost or your production domains
const isLocalhost = host.includes('localhost');
const isProductionDomain = host.includes('rainframe.xyz') || host.includes('raindex.finance');
const isVercelDomain = host.includes('vercel.app');
if (isLocalhost) {
if (domains.length > 1) {
// localhost with subdomain
subdomain = domains[0];
} else {
// localhost without subdomain
subdomain = 'raindex';
}
} else if (isProductionDomain) {
if (domains.length > 2) {
// Production domain with subdomain
subdomain = domains[0];
} else {
// Production root domain
subdomain = 'raindex';
}
} else if (isVercelDomain) {
subdomain = 'raindex';
}
if (subdomain) {
// Exclude certain paths from rewriting
if (
url.pathname.startsWith('/_images') ||
url.pathname.startsWith('/my-strategies') ||
url.pathname.startsWith('/favicon')
) {
return NextResponse.next();
}
url.pathname = `/${subdomain}${url.pathname}`;
return NextResponse.rewrite(url);
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!_next|api|static|favicon.ico).*)']
};