5cb70cd307
- next.config.js: skipTrailingSlashRedirect évite le 308 sur /api/check-domain - middleware.ts: exclut /api/check-domain du rewrite pour ne pas interférer - Caddy peut maintenant valider les sous-domaines d'instances auprès du serveur
29 lines
883 B
TypeScript
29 lines
883 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
const MAIN_DOMAIN = process.env.MAIN_DOMAIN || "alfrednobel.edudeploy.com";
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const host = req.headers.get("host") || "";
|
|
const cleanHost = host.split(":")[0];
|
|
|
|
if (cleanHost === MAIN_DOMAIN || cleanHost === `www.${MAIN_DOMAIN}`) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
if (!cleanHost.endsWith(`.${MAIN_DOMAIN}`)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const pathname = req.nextUrl.pathname;
|
|
const search = req.nextUrl.search;
|
|
|
|
// Rewrite to the internal proxy API while preserving the original host header
|
|
const rewriteUrl = new URL(`/api/proxy${pathname}${search}`, req.url);
|
|
return NextResponse.rewrite(rewriteUrl);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api/proxy|api/check-domain|_next|static|favicon.ico).*)",],
|
|
};
|