From a788931828cd624aa6781a0185d9b46c779e8f27 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Wed, 25 Feb 2026 19:25:34 +0330 Subject: [PATCH] middleware dkala --- src/middleware.ts | 51 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 0ab2d42..e9524ed 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,11 +1,48 @@ -import { NextRequest } from 'next/server'; +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export function middleware(request: NextRequest) { - +// Map host → tenant path +const HOST_MAP: Record = { + "avitaprotein.com": "/avita", + // دامنه‌های جدید اینجا اضافه می‌شوند +}; + +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); } -// only applies this middleware to files in the app directory export const config = { - matcher: '/((?!api|static|favicon.ico|.*\\..*|_next).*)' -}; \ No newline at end of file + matcher: "/:path*", +};