-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathproxy.ts
More file actions
32 lines (27 loc) · 1.19 KB
/
Copy pathproxy.ts
File metadata and controls
32 lines (27 loc) · 1.19 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
import { NextRequest, NextResponse } from "next/server";
import { parseWebsiteReverseHost } from "@/lib/parse-website-reverse-host";
export function proxy(request: NextRequest) {
const host = request.headers.get("host") ?? "";
const hostname = host.split(":")[0]?.toLowerCase() ?? "";
// Redirect www.something.gitreverse.com → something.gitreverse.com
// (Vercel nameservers issue individual certs for each subdomain, so the
// SSL handshake succeeds, then we strip the www. here)
if (hostname.endsWith(".gitreverse.com")) {
const sub = hostname.slice(0, -".gitreverse.com".length);
if (sub.startsWith("www.")) {
const withoutWww = sub.slice(4);
const url = request.nextUrl.clone();
url.host = `${withoutWww}.gitreverse.com`;
return NextResponse.redirect(url, 301);
}
}
const parsed = parseWebsiteReverseHost(host);
if (!parsed) return NextResponse.next();
const url = request.nextUrl.clone();
url.pathname = `/website/${encodeURIComponent(parsed.slug)}`;
url.searchParams.set("url", parsed.targetUrl);
return NextResponse.rewrite(url);
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|opengraph-image).*)"],
};