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
15 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.@Asian black bear Yes. Check the platform template: https://vercel.com/templates/next.js/platforms-starter-kit
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?
@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?
yolocatOP
it works perfectly when running locally too, this doesn't require vercel at all
@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?
Asian black bear
The gist is that the middleware just rewrites
tenant.domain.com/path
in most cases to /[tenant]/path
matching your hierarchy in the file system.Bombay-duck
I got it to match the main domain, but I'm now having fun with apache trying to get it to route to the subdomains.
Asian black bear
You just send all requests of the shape
*.domain.com
to the Next app and let it deal with subdomains.Bombay-duck
I have my configuration set up like that, but when I go to a subdomain it fails to load anything
I'm stuck at work overnight so I'll have to test tomorrow
Bombay-duck
I got it working, thanks for the help! My tired brain messed up the folder setup in the middleware
@Bombay-duck I got it working, thanks for the help! My tired brain messed up the folder setup in the middleware
Asiatic Lion
how did you figure it out?