Next.js Discord

Discord Forum

How can I rewrite and redirect in the same middleware?

Unanswered
Black carp posted this in #help-forum
Open in Discord
Avatar
Black carpOP
I have multitenant app with localization and my current pathname looks like this: /[locale]/* and I want my client to see the locale in the pathname, in my middelware I make sure there's locale in the pathname and if there isn't I add English locale by redirecting the user in the middleware

Now I need to get the domain of my tenant so I wrapped my app with another directory so my pathname looks like this: /[domain]/[locale]/* and unlike the locale pathname this one (the domain pathname) should never be visible to the client, so in the middleware I get the domain from the request object and rewrite it in the pathname, this way the client never sees the domain in the pathname

My problem is that I now cannot use both pathnames of locale (must be visible to the client) and domain (must NOT be visible to the client) in the same middleware because sometimes I need to redirect the user to the default locale (so they would always have locale in URL) and I always need to rewrite the domain from the request object into the domain pathanme...

So my question is, is there a way to do both redirect and rewrite in the same middleware and if not, is there a way to create more then one middleware and stack them one after the other?

0 Replies