← All tips
Deploy1 min read

Serve two sites from one deploy with host routing

You don't need separate repos to run multiple sites. Detect the request host in a proxy and rewrite to a per-site namespace:

export function proxy(request: NextRequest) {
  const host = request.headers.get('host') ?? ''
  if (host.includes('secondsite')) {
    const url = request.nextUrl.clone()
    url.pathname = `/second${url.pathname}`
    return NextResponse.rewrite(url)
  }
  return NextResponse.next()
}

Warning

Keep your primary site at the root path so its output never changes — only rewrite the secondary hosts.