-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INV-50 와일드카드 서브도메인 rewrite 라우팅 처리 (#25)
* remove: redirect 로직 * feat: middleware에서 wildcard 서브도메인 rewrite로직 * chore: 환경변수 추가 * 코드정리 * chore: ROOT_DOMAIN 환경변수 제거
- Loading branch information
Showing
3 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
redirects: async () => { | ||
return []; | ||
}, | ||
}; | ||
const nextConfig = {}; | ||
|
||
export default nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page({ params }: { params: { subdomain: string } }) { | ||
return <div>Subdomain: {params.subdomain}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |