On this page
Custom domains let customers serve your application from a hostname they control, such as portal.customer.example. For multi tenant custom domain URL routing, the core task is to map that hostname to the right account without confusing routing with authorization.
This guide compares hostname and path routing, then walks through domain verification, DNS, TLS, and a defensive lookup design. Provider-specific setup is intentionally omitted: no current hosting-provider documentation was available for verification in this run.
Key Takeaways
- Use a verified, normalized hostname as a routing key—not as proof that a user can access the tenant.
- Path-based routing is simpler to operate; customer-owned domains add DNS, certificate, verification, and lifecycle work.
- Activate a custom domain only after ownership verification, DNS configuration, and TLS provisioning have succeeded.
- Reject unknown or inactive hosts, and use trusted proxy configuration rather than blindly trusting forwarded headers.
- Immediately disable routing when a domain is removed, and plan for stale DNS and safe reassignment.
Hostname Routing vs. Path Routing
With path routing, tenants share a host and have distinct paths, such as app.saas.example/acme and app.saas.example/other. With hostname routing, the host selects the tenant: acme.saas.example or a customer-owned hostname such as portal.customer.example.
| Decision | Path-based | Hostname-based |
|---|---|---|
| Tenant selection | Extract a path segment | Look up the request hostname |
| Customer branding | Shared hostname remains visible | A customer-owned hostname can be displayed |
| Operations | One primary DNS and TLS configuration | Custom domains require verification, DNS, TLS, and removal workflows |
| Application design | Links and redirects must preserve the tenant path | Links, cookies, redirects, and authentication flows must account for multiple hosts |
Path routing is a reasonable default when custom branding is not essential. Host routing is useful when customers need their own domains, but it requires coordination with customer DNS administrators and with your hosting or edge infrastructure.
Neither approach authorizes users by itself. A user who can guess another tenant's path or send a request with a different host must not gain access to that tenant's data.
Advertisement
Set Up and Verify a Customer Domain
Treat a custom domain as a resource with a lifecycle, not just a string on an account record.
- Register the requested hostname. Normalize it, check that it is eligible for your service, and record it as pending. Enforce a unique claim on the normalized hostname so two tenants cannot activate it simultaneously.
- Verify control. Issue a unique, unpredictable verification token and instruct the customer to publish it using the verification method supported by your infrastructure, commonly a DNS TXT record. Check the expected record before allowing activation. Bind the verification attempt to the requesting tenant and domain; tokens should expire or be rotated according to your policy.
- Configure traffic DNS. Give the customer the exact records required by your hosting provider. Apex domains and subdomains can have different DNS options and provider requirements; do not assume that instructions for one apply to the other.
- Provision TLS. Your edge or hosting provider must be able to serve a valid certificate for the customer's hostname. The TLS handshake happens before the application receives an HTTP request, so an application database lookup alone cannot provision or select a certificate. A certificate for your own service's wildcard domain does not cover an unrelated customer-owned domain.
- Activate routing. Mark the hostname active only when verification and the required traffic and certificate checks have succeeded. Surface pending and failed states to the customer, since DNS propagation and certificate issuance are asynchronous.
Verification establishes control at the time of the check; it is not permanent proof. Define when to recheck domains, especially before reassignment, and monitor certificate renewal and DNS changes. Consult your selected provider's current custom-domain documentation for its actual DNS targets, verification procedure, certificate behavior, and limits before implementing these steps.
Normalize the Hostname Before Lookup
Use the same documented normalization policy when a customer registers a domain and when a request arrives. At minimum:
- Parse the request authority with a well-defined parser rather than splitting on
:; ports and IP address syntax make naive splitting unreliable. - Convert internationalized domain names consistently to their ASCII form, lowercase the result, and define how to handle a trailing DNS dot. Reject malformed names.
- Separate the hostname from any port, and have the edge enforce the ports and protocols your service accepts.
- Decide explicitly whether IP literals, single-label names, and reserved service hostnames are allowed. Customer custom-domain systems commonly reject them.
- Store the normalized hostname under a uniqueness constraint. Do not use suffix matching:
portal.customer.example.attacker.exampleis notportal.customer.example.
The incoming host is client-controlled input. Ensure your edge rejects malformed or conflicting authority information. If a proxy supplies a forwarded host, accept it only from trusted proxies configured to remove client-supplied forwarding headers and set their own. Otherwise, use the authority delivered by the trusted server interface. Do not choose a header simply because it exists.
Advertisement
Look Up the Tenant, Then Authorize the User
The following SQL is an illustrative lookup for a schema with custom_domains and tenants tables. $1 must be bound as a parameter by the database client; it is the normalized hostname obtained through the trusted request-processing path described above. The domain's unique constraint should be on normalized_hostname.
SELECT t.id
FROM custom_domains AS d
JOIN tenants AS t ON t.id = d.tenant_id
WHERE d.normalized_hostname = $1
AND d.verification_status = 'verified'
AND d.routing_status = 'active'
AND t.status = 'active';
If there is no matching row, return a generic error rather than falling back to a default tenant. This example assumes TLS readiness is checked before routing_status becomes active. It illustrates tenant selection only; the schema, normalization, proxy configuration, and authentication code are not included, and the query has not been executed here.
After selecting the tenant, authenticate the user and check their membership and permissions for that tenant before reading or modifying tenant data. Scope database queries and background operations to the authorized tenant ID, not merely to a user-supplied hostname. For public pages, explicitly define which resources are public instead of treating an active domain as permission to expose the whole account.
Use the stored, verified canonical domain when generating absolute links or redirects. Reflecting an unchecked request host into password-reset links, redirects, or emails can send users to an attacker-controlled destination.
Handle Domain Changes and Removal
For a domain change, register and verify the new hostname before making it active. Decide whether the old and new names will overlap temporarily, and ensure links and authentication callbacks use the intended canonical host. Redirect from an old hostname only while your service is still authorized to serve it; don't assume control after the customer removes it or transfers it.
When a customer removes a domain:
- Disable its tenant mapping immediately and invalidate routing caches or edge configuration as needed. Unknown or inactive hosts should not reach tenant content.
- Remove provider-side routing and certificate configuration according to the provider's instructions. Tell the customer which DNS records to remove.
- Account for stale DNS records. A dangling record pointing at a released service resource can create a takeover risk, depending on the provider's behavior; coordinate deprovisioning and monitor or reserve resources where appropriate.
- Clear or expire verification material and require a fresh control check before any tenant can claim the hostname again. Use a hold or review process if your threat model calls for it.
Also plan for delayed cache invalidation, certificate renewal failures, and customers losing domain ownership. A database status change is not sufficient if an edge cache continues to route requests using an older mapping.
Common Mistakes
- Treating the host as authentication. Anyone can send a request naming a host; tenant membership and permissions remain separate checks.
- Trusting arbitrary forwarded headers. A client may be able to supply them unless the proxy boundary is configured correctly.
- Activating on DNS configuration alone. A record pointing to your service does not replace your ownership-verification and TLS-readiness checks.
- Using inconsistent hostname forms. Case, internationalized names, ports, and trailing dots can cause failed lookups or duplicate claims if handled differently at registration and request time.
- Ignoring browser and identity boundaries. A cookie scoped to your SaaS domain is not automatically shared with an unrelated customer domain. Review cookie scope, login flows, redirect allowlists, and any identity-provider callback configuration for each supported host.
Best Practices
Keep domain states and failure reasons visible to operators and customers. Log domain verification, activation, removal, and reassignment events without recording verification secrets. Test unknown hosts, malformed authorities, duplicate claims, pending domains, removed domains, and users who are authenticated but lack access to the selected tenant.
Use exact hostname matches and centralized tenant resolution. Make authorization and tenant scoping mandatory after resolution, and test cache invalidation as part of domain removal—not just as a deployment concern.
Conclusion
Hostname routing can provide customer branding, but it adds an ownership and infrastructure lifecycle that path routing largely avoids. Resolve only verified, active hosts to tenants, then independently authorize each request. Design removal and reassignment with the same care as initial setup.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.