Next.js Discord

Discord Forum

Can I serve for multiple subdomains with a single Next.js app?

Answered
yolocat posted this in #help-forum
Open in Discord
Avatar
Hi! So long story short, I'd like to point x.example.com/... to <next server>/x/... (in reality I'd like to intercept the x inbetween to get an id from my database, but yeah). Is there any way to do such a thing (presumably in middleware)?
Answered by Asian black bear
View full answer

7 Replies

Avatar
Asian black bear
Answer
Avatar
Asian black bear
If I'm not mistaken (haven't checked the code yet) subdomains are rewritten in the middleware to map onto /app/[tenant]/... then your code can consume it as a path parameter.
Avatar
thanks, this was exactly what I was looking for! I do have one issue though, when admin.example.com (or app. as per the example) doesn't have a page but [tenant].example.com does, the router thinks that admin is another tenant
app:
  [tenant]:
    layout.tsx
    a:
      page.tsx
  admin:
    layout.tsx
    b:
      page.tsx
accessing example.com/admin/a (rewritten from admin.example.com/a) it matches the page under [tenant], I would like that to simply generate a 404 instead
Avatar
[...404]/page.tsx did the trick :)
Avatar
Bombay-duck
How can I do this without having to use vercel? I use my own VPS because I host other things on it as well like discord bots and such. The guide on the platforms starter kit just walks you through doing it for vercel. Would I just need to start digging through source code on the github to find the rewrite mechanism?