Setting a different host for requests in Next.js
Unanswered
Large garden bumble bee posted this in #help-forum
Large garden bumble beeOP
In Next.js, does anyone know how to change the base url? The use case is in my middleware function
The problem is, I updated
Is there any way to make it such that it is sending the requests through my aliased url rather than
function isExternalHost(url: URL) {
return url.host !== process.env.NEXT_PUBLIC_SECURE_HOST_URL;
}
function middleware(request: NextRequest) {
const redirect = request.nextUrl.searchParams.get("redirect");
const redirectUrl = new URL(redirect, request.url);
if (isExternalHost(redirectUrl)) {
console.warn("can't redirect :(");
return NextResponse.next({ request });
}
return NextResponse.redirect(redirectUrl);
}The problem is, I updated
/etc/hosts to https://localhost.<my-domain>:3000 but next is intercepting requests as http://localhost:3000Is there any way to make it such that it is sending the requests through my aliased url rather than
http://localhost:3000?2 Replies
@Large garden bumble bee In Next.js, does anyone know how to change the base url? The use case is in my middleware function
typescript
function isExternalHost(url: URL) {
return url.host !== process.env.NEXT_PUBLIC_SECURE_HOST_URL;
}
function middleware(request: NextRequest) {
const redirect = request.nextUrl.searchParams.get("redirect");
const redirectUrl = new URL(redirect, request.url);
if (isExternalHost(redirectUrl)) {
console.warn("can't redirect :(");
return NextResponse.next({ request });
}
return NextResponse.redirect(redirectUrl);
}
The problem is, I updated `/etc/hosts` to `https://localhost.<my-domain>:3000` but next is intercepting requests as `http://localhost:3000`
Is there any way to make it such that it is sending the requests through my aliased url rather than `http://localhost:3000`?
West African Lion
It seems you want to customize domain in Next.js middleware.
How about
Is it impossible to give the
How about
next.config.js?Is it impossible to give the
https://localhost.<my-domain>:3000 directly to the value field of headers?So you can control it with
X-Forwarded-Host and X-Forwarded-Proto keys.