diff --git a/next.config.mjs b/next.config.mjs index a74a6eb..4678774 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,8 +1,4 @@ /** @type {import('next').NextConfig} */ -const nextConfig = { - redirects: async () => { - return []; - }, -}; +const nextConfig = {}; export default nextConfig; diff --git a/src/app/(playground)/pg/[subdomain]/page.tsx b/src/app/(playground)/pg/[subdomain]/page.tsx new file mode 100644 index 0000000..4721981 --- /dev/null +++ b/src/app/(playground)/pg/[subdomain]/page.tsx @@ -0,0 +1,3 @@ +export default function Page({ params }: { params: { subdomain: string } }) { + return
Subdomain: {params.subdomain}
; +} diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..aff80a5 --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,29 @@ +import type { NextRequest } from "next/server"; +import { NextResponse } from "next/server"; + +export const config = { + matcher: ["/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)"], +}; + +export function middleware(request: NextRequest) { + const url = request.nextUrl; + const hostname = request.headers.get("host")!; + const searchParams = request.nextUrl.searchParams.toString(); + + const path = `${url.pathname}${ + searchParams.length > 0 ? `?${searchParams}` : "" + }`; + + const subDomain = hostname.split(".")[0]; + console.log("subDomain", subDomain, "hostname", hostname); + + switch (true) { + case subDomain !== hostname: + return NextResponse.rewrite( + new URL(`/pg/${subDomain}${path}`, request.url), + ); + + default: + return NextResponse.rewrite(new URL(`${path}`, request.url)); + } +}