Redirect and Middleware rewrites
Hi guys, I got a question that might be silly or not but is regarding the NextJS redirect and rewrites in the middleware in v
14.2.4
.
So I have a multi-tenant app, and in my middleware is where I rewrite my NextResponse (“NextResponse.rewrite()”). I'm using the tenant as a subdomain, so let's say that my local url looks like this: http://mytenant.localhost:3000/
. So I have a case where I'm using redirect()
from next-navigation
to send the user to redirect('/the-redirect-url/foo/faa')
. In the middleware when I want to grab that subdomain from the req.headers.get('host')
I just got localhost
as a host, while if I just reload a page from my subdomain, I do get mytenant.localhost
as the host. So, what'm I doing wrong? Any hints or modifications? Did I miss something?
Also, because I’m rewriting the response in my middleware, should I be using “x-forwarded-host” instead of “host”? If I do, it works fine, so it is safe?
(I’m using the redirect in a server action)0 Replies