-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathmiddleware.ts
More file actions
43 lines (37 loc) · 1.32 KB
/
middleware.ts
File metadata and controls
43 lines (37 loc) · 1.32 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Short URL domain
const SHORT_URL_DOMAIN = "aigl.ink";
export function middleware(request: NextRequest) {
const host = request.headers.get("host") || "";
const pathname = request.nextUrl.pathname;
// Check if request is from the short URL domain (aigl.ink)
if (host === SHORT_URL_DOMAIN || host === `www.${SHORT_URL_DOMAIN}`) {
// Root path on aig.link -> redirect to main site tools page
if (pathname === "/") {
return NextResponse.redirect(
new URL("/tools/url-shortener", "https://articleideagenerator.com"),
);
}
// Any other path on aig.link -> treat as short code
// Rewrite to /r/[code] which handles the redirect with analytics
const code = pathname.slice(1); // Remove leading slash
if (code && !code.includes("/")) {
return NextResponse.rewrite(new URL(`/r/${code}`, request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder files
* - API routes (handled separately)
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\..*|api).*)",
],
};