One Next.js App, Three Hostnames: Serving Buyers, Vendors and Admins from One Codebase / Nov 17, 2026

8 min readBy Emmanuel Akinfulubi
One Next.js App, Three Hostnames: Serving Buyers, Vendors and Admins from One Codebase

AduraMart is a faith-goods marketplace I work on. It has three audiences: buyers on aduramart.com, vendors on vendors.aduramart.com and staff on admin.aduramart.com. They are not three apps. They are one Next.js app, deployed once, with a small piece of routing that reads the hostname and decides which part of the app to show.

This post explains that pattern, the one gap it opens that you have to close, and the line it should never cross: routing is not security.

Why not three apps?

Three audiences sound like three projects. In practice they share a lot: the design system, the API client, the auth flow, the money formatting, the product types. Three repos means three copies of all of that drifting apart, three pipelines and three things to keep updated.

One app gives you:

  • One build and one deploy. A shared component fix reaches all three at once.
  • Shared code without a package to publish. The storefront and the vendor hub import the same formatNaira().
  • Separate bundles anyway. The App Router splits JavaScript by route, so the code for the admin pages is not sent to someone browsing the storefront.

The cost is that one bad deploy takes all three down together. For AduraMart that trade was worth it.

The folder layout

Each extra hostname gets a folder under app/. The storefront lives at the root:

app/
  page.jsx              aduramart.com/
  products/[slug]/      aduramart.com/products/...
  vendor/
    page.jsx            vendors.aduramart.com/
    orders/page.jsx     vendors.aduramart.com/orders
  admin/
    page.jsx            admin.aduramart.com/
    payouts/page.jsx    admin.aduramart.com/payouts

Visitors never see /vendor or /admin in the address bar. They see vendors.aduramart.com/orders, and the routing quietly renders app/vendor/orders.

One note on naming: you can't use _vendor to hide the folder. In the App Router a folder starting with an underscore is private and has no routes at all. A plain folder name is the normal approach. You hide it with the routing, as below.

The routing: a rewrite, not a redirect

A redirect sends the browser to a new URL. A rewrite keeps the URL the visitor typed and serves a different page behind it. Hostname routing needs a rewrite.

In Next.js this lives in one file at the project root. AduraMart is on Next.js 15, where the file is middleware.js and the function is called middleware. Next.js 16 renamed both to proxy, and the behaviour is the same. Here is a simplified version of the pattern in the Next.js 16 form:

// proxy.js (Next.js 16). On Next.js 15, name the file middleware.js and the function middleware.
import { NextResponse } from "next/server";
 
// "localhost" in development, so vendors.localhost:3000 works.
const ROOT = process.env.ROOT_DOMAIN ?? "aduramart.com";
 
// Subdomain -> folder under app/. Anything else is the storefront.
const SECTIONS = new Map([
  ["vendors", "/vendor"],
  ["admin", "/admin"],
]);
const INTERNAL = [...SECTIONS.values()];
 
function sectionFor(host) {
  if (!host.endsWith(`.${ROOT}`)) return null;
  return SECTIONS.get(host.slice(0, -(ROOT.length + 1))) ?? null;
}
 
export function proxy(request) {
  const host = (request.headers.get("host") ?? "").split(":")[0].toLowerCase();
  const { pathname } = request.nextUrl;
 
  // Close the back door: the internal folders are never reachable by path.
  if (INTERNAL.some((p) => pathname === p || pathname.startsWith(`${p}/`))) {
    return new NextResponse(null, { status: 404 });
  }
 
  const section = sectionFor(host);
  if (!section) return NextResponse.next(); // the storefront
 
  const url = request.nextUrl.clone();
  url.pathname = pathname === "/" ? section : `${section}${pathname}`;
  return NextResponse.rewrite(url);
}
 
export const config = {
  // Skip static assets so the proxy only runs for pages and API routes.
  matcher: ["/((?!_next/|favicon.ico|robots.txt|sitemap.xml).*)"],
};

A few choices in there are deliberate:

  • A Map, not a plain object. With an object, a request to constructor.aduramart.com would find JavaScript's built-in constructor and route somewhere strange. A Map only holds what you put in it.
  • The suffix check includes the dot. .endsWith(".aduramart.com") means evil-aduramart.com and admin.aduramart.com.evil.com both fall through to the storefront.
  • Lowercase and strip the port. Hostnames are not case-sensitive, and a Host header can carry :443.

These are the cases worth testing before you ship it:

RequestResult
aduramart.com/products/robeStorefront, no rewrite
vendors.aduramart.com/ordersRewritten to /vendor/orders
admin.aduramart.com/payouts?run=4Rewritten to /admin/payouts?run=4
aduramart.com/admin/payouts404
vendors.aduramart.com/vendor/orders404
constructor.aduramart.com/Storefront
admin.aduramart.com.evil.com/Storefront

The back door you have to close

Without the 404 block near the top, the routing has a hole. Someone visits aduramart.com/admin/payouts. The storefront host doesn't match any section, so the request passes straight through, and Next.js happily renders app/admin/payouts on the public storefront domain.

The admin page would still need a logged-in admin to show anything useful, as the next section explains. But "it would probably fail safely" is not a design. Blocking the internal prefixes on every host makes each section reachable one way only: through its own hostname.

Routing is not security

This is the most important line in the post. A rewrite decides which page renders, not who is allowed to see it. The Next.js docs say the same thing about Proxy: it's fine for quick checks such as redirecting a signed-out visitor, but it should not be your session management or authorisation.

On AduraMart the line is drawn clearly:

  • The frontend holds no database credentials. Every read is a request to a separate NestJS API, which owns the database and every money decision.
  • The API is closed by default. Every route passes three global guards in order: throttle, authenticate, authorise. If someone forgets to mark a new endpoint, it is locked, not exposed.

So even if a vendor somehow loaded an admin page, the data behind it would refuse them. Put your real checks where the data lives. Treat the hostname routing as a convenience for humans.

Cookies: keep each hostname's session to itself

It is tempting to set the session cookie with Domain=aduramart.com so one login works on every subdomain. That also sends the cookie to every subdomain, including any you add later.

A safer default is a host-only cookie: leave the Domain attribute off, and the browser only sends the cookie back to the exact hostname that set it. A buyer's session stays on the storefront, and an admin's stays on the admin host. Prefixing the name with __Host- makes the browser enforce that. A __Host- cookie must be Secure, must use Path=/ and may not have a Domain at all.

Small things that come with three hostnames

Search engines. Only the storefront should be indexed. Mark the vendor and admin layouts noindex:

// app/admin/layout.jsx (and the same in app/vendor/layout.jsx)
export const metadata = {
  robots: { index: false, follow: false },
};

Keep the sitemap to storefront URLs, since those are the only pages you want found.

Links. Inside a section, ordinary links just work. <Link href="/orders"> on the vendor hub goes to vendors.aduramart.com/orders, which the proxy rewrites. Links that cross sections, like "Sell on AduraMart" on the storefront, need the full URL: https://vendors.aduramart.com.

Local development. Most browsers send any name ending in .localhost to your own machine. Set ROOT_DOMAIN=localhost in development and open vendors.localhost:3000. From a terminal you can fake the host instead:

curl -H "Host: vendors.localhost:3000" http://localhost:3000/orders

DNS and TLS. Each hostname needs a DNS record pointing at the server and a certificate. On a self-hosted setup like Coolify, add all three domains to the same application and the proxy in front requests a certificate for each. I covered that setup in Deploying a Next.js App on Coolify.

When three apps would be the better call

One app stops being the right answer when:

  • The teams are separate and release on different schedules.
  • The sections share almost nothing, such as a marketing site and an internal tool built on different stacks.
  • One section needs much stricter isolation. An admin console that should only be reachable from a private network is easier to protect as its own deployment.

For a marketplace where one small team builds all three faces of the same product, one app with hostname routing is the simpler system to run.

The short version

  • One Next.js app can serve several hostnames with a rewrite in proxy.js, or middleware.js before Next.js 16.
  • Map subdomains with a Map and an exact suffix check, and block the internal folders on every host.
  • The rewrite is routing, not security. Check permissions in the API that owns the data.
  • Use host-only session cookies, keep the private hosts out of search, and use absolute URLs between sections.

Further reading: the Next.js docs on Proxy and rewrites.

Frequently asked questions

Can one Next.js app serve different subdomains?
Yes. A proxy file (called middleware before Next.js 16) reads the Host header on every request and rewrites the path to a different folder in the app, so vendors.example.com/orders can render app/vendor/orders while the browser still shows the original URL.
What is the difference between middleware and proxy in Next.js?
Only the name. Next.js 16 renamed middleware.ts to proxy.ts and the exported function from middleware to proxy. The behaviour is the same, so a hostname rewrite written for Next.js 15 moves across with a rename.
Is a rewrite enough to protect an admin area?
No. A rewrite decides which page renders, not who may see it. Next.js's own docs say Proxy should not be used as a full session management or authorisation solution. Check the user's role on the server for every request, ideally in the API that owns the data.
How do I test subdomains locally?
Most browsers send any name ending in .localhost to your own machine, so vendors.localhost:3000 reaches your dev server. Set the root domain to localhost in development, or send a Host header with curl.