Created
October 19, 2023 15:05
-
-
Save ericc59/18ac70d276405fe5c832697f7467fb77 to your computer and use it in GitHub Desktop.
Something and something else middleware
This file contains hidden or 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
import { NextRequest, NextResponse } from "next/server"; | |
export default async function middleware(req: NextRequest) { | |
const url = req.nextUrl; | |
// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000) | |
const hostname = req.headers.get("host")! | |
const path = url.pathname; | |
// If you're using the app dir, a request to somethingelse.com/special-somethingelse-page would rewrite to $PROJECT_ROOT/app/somethingelse/special-somethingelse-page/page.tsx | |
if ( | |
hostname === "somethingelse.localhost:3000" || | |
hostname === "somethingelse.com" | |
) { | |
return NextResponse.rewrite(new URL(`/somethingelse${path}`, req.url)) | |
} | |
// If you're using the app dir, a request to something.com/something-page would rewrite to $PROJECT_ROOT/app/something/special-something-page/page.tsx | |
if ( | |
hostname === "localhost:3000" || | |
hostname === "something.com" | |
) { | |
return NextResponse.rewrite(new URL(`/something${path}`, req.url)); | |
} | |
// Allow all other requests to continue as normal | |
return NextResponse.next() | |
} | |
export const config = { | |
matcher: [ | |
"/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)", | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment