Next.js Discord

Discord Forum

Problem with SubDomains (Multitenancy)

Unanswered
Rohu posted this in #help-forum
Open in Discord
RohuOP
I am trying to add subdomains to my project where I am trying to have [slug].domainname.com and then also just the regular dominname.com, I structured my code app router to be app, then api, [subdomain], and home. then i created middleware based on the platforms template code https://github.com/vercel/platforms/tree/main
Heres the middleware:



export default function middleware(req) {
const url = req.nextUrl;


const hostname = req.headers.get("host").replace(".localhost:3000", .${process.env.NEXT_PUBLIC_ROOT_DOMAIN});
console.log("Determined Hostname:", hostname);

// Get the pathname of the request (e.g. /, /about, /blog/first-post)
const path = url.pathname;


// Rewrite root application to /home folder
if (
hostname === "localhost:3000" ||
hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
return NextResponse.rewrite(new URL(home${path}, req.url));
}


// Rewrite everything else to /[domain]/[path] dynamic route
return NextResponse.rewrite(new URL(/${hostname}${path}, req.url));
}


everything works perfectly fine in development, but when I publishing to vercel it does not recognize domainname.com, but recognizes everything else such as domainname.com/[slug] . even weirder, if i use next link back to domainname.com, it shows up. If i refresh it goes back to 404 error. subdomains is works as expected.

Anyone have a fix on why its not showing

1 Reply

RohuOP
follow up, it was not a problem with my middleware, even though there was tweaks needed to be made, the problem is with my next auth, and im trying to figure that out.