Can I serve for multiple subdomains with a single Next.js app?
Answered
yolocat posted this in #help-forum
yolocatOP
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
Yes. Check the platform template: https://vercel.com/templates/next.js/platforms-starter-kit
7 Replies
Asian black bear
Yes. Check the platform template: https://vercel.com/templates/next.js/platforms-starter-kit
Answer
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.yolocatOP
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 tenantapp:
[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 insteadyolocatOP
[...404]/page.tsx
did the trick :)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?