INTEGRITY Cloudflare Docs

Hostname routing

You can use dynamic dispatch Workers to route millions of vanity domains or subdomains to Workers without hitting traditional route limits. These hostnames can be subdomains under your managed domain (e.g. customer1.saas.com) or vanity domains controlled by your end customers (e.g. mystore.com), which can be managed through custom hostnames.

Configure a wildcard Route (*/*) on your SaaS domain (the domain where you configure custom hostnames) to point to your dynamic dispatch Worker. This allows you to:

If you'd like to exclude certain hostnames from routing to the dispatch Worker, you can either:

Setup

To set up hostname routing with a wildcard route:

  1. Configure custom hostnames: Set up your domain and custom hostnames using Cloudflare for SaaS
  2. Set the fallback origin: Set up a fallback origin server, this is where all custom hostnames will be routed to. If you’d like to route them to separate origins, you can use a custom origin server. Requests will route through the Worker before reaching the origin. If the Worker is the origin then place a dummy DNS record for the fallback origin (e.g., A 192.0.2.0).
  3. Configure DNS: Point DNS records (subdomains or custom hostname) via CNAME record to the saas domain. If your customers need to proxy their apex hostname (e.g. example.com) and cannot use CNAME records, check out Apex Proxying.
  4. Create wildcard route: Add a */* route on your platform domain (e.g. saas.com) and associate it with your dispatch Worker.
  5. Implement dispatch logic: Add logic to your dispatch Worker to route based on hostname, lookup mappings stored in Workers KV, or use custom metadata attached to custom hostnames.

Example dispatch Worker

export default {
	async fetch(request, env) {
		const hostname = new URL(request.url).hostname;

		// Get custom hostname metadata for routing decisions
		const hostnameData = await env.KV.get(`hostname:${hostname}`, {
			type: "json",
		});

		if (!hostnameData?.workerName) {
			return new Response("Hostname not configured", { status: 404 });
		}

		// Route to the appropriate user Worker
		const userWorker = env.DISPATCHER.get(hostnameData.workerName);
		return await userWorker.fetch(request);
	},
};

Subdomain routing

If you're only looking to route subdomain records (e.g. customer1.saas.com), you can use a more specific route (*.saas.com/*) to route requests to your dispatch Worker.

Setup

To set up subdomain routing:

  1. Create an orange-clouded wildcard DNS record: *.saas.com that points to the origin. If the Worker is the origin then you can use a dummy DNS value (for example, A 192.0.2.0).
  2. Set wildcard route: *.saas.com/* pointing to your dispatch Worker
  3. Add logic to the dispatch Worker to route subdomain requests to the right Worker.

Example subdomain dispatch Worker

export default {
	async fetch(request, env) {
		const url = new URL(request.url);
		const subdomain = url.hostname.split(".")[0];

		// Route based on subdomain
		if (subdomain && subdomain !== "saas") {
			const userWorker = env.DISPATCHER.get(subdomain);
			return await userWorker.fetch(request);
		}

		return new Response("Invalid subdomain", { status: 400 });
	},
};

O2O Behavior

When your customers are also using Cloudflare and point their custom domain to your SaaS domain via CNAME (for example, mystore.comsaas.com), Worker routing behavior depends on whether the customer's DNS record is proxied (orange cloud) or DNS-only (grey cloud). Learn more about O2O setups

This can cause inconsistent behavior when using specific hostname routes:

Since you may not have control over your customer's DNS proxy settings, we recommend using */* wildcard route to ensure routing logic always works as expected, regardless of how DNS is configured.

Worker invocation across route configurations and proxy modes

The table below shows when Workers are invoked based on your route pattern and the customer's DNS proxy settings:

Route Pattern Custom Hostname (Orange Cloud) Custom Hostname (Grey Cloud)
*/* (Recommended)
Target hostname route
Custom hostname route