52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import type { NextRequest } from "next/server";
|
|
import { NextResponse } from "next/server";
|
|
|
|
// Map host → tenant path
|
|
const HOST_MAP: Record<string, string> = {
|
|
"theboote.tahavol-mr.ir": "/boote",
|
|
"thesun.tahavol-mr.ir": "/suncafe",
|
|
"passataplus.ir": "/passata",
|
|
"cookieca.ir": "/cookieca",
|
|
// دامنههای جدید اینجا اضافه میشوند
|
|
};
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const host = req.headers.get("host") || "";
|
|
const tenantPath = HOST_MAP[host.toLowerCase()] || "";
|
|
|
|
if (!tenantPath) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const pathname = req.nextUrl.pathname;
|
|
|
|
// مسیرهای api و استاتیک را rewrite نکن
|
|
if (
|
|
pathname.startsWith("/api") ||
|
|
pathname.startsWith("/_next") ||
|
|
pathname.includes(".")
|
|
) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// اگر کاربر ریشه دامنه را باز کرده، به مسیر tenant ریدایرکت کن (آدرسبار /passata و غیره نشون بده)
|
|
if (pathname === "/") {
|
|
const url = req.nextUrl.clone();
|
|
url.pathname = tenantPath;
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
// اگر مسیر از قبل با tenant یکی است (مثلاً /passata یا /passata/menu)، rewrite نکن
|
|
if (pathname === tenantPath || pathname.startsWith(tenantPath + "/")) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const url = req.nextUrl.clone();
|
|
url.pathname = `${tenantPath}${pathname}`;
|
|
return NextResponse.rewrite(url);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: "/:path*",
|
|
};
|