Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Cloudflare Pages middleware can inspect the hostname on an incoming request and apply application-defined behavior for different subdomains. Add a root-level functions/_middleware.js, read the hostname from context.request.url, and match it against hostnames your application explicitly supports. For a matching host, select the appropriate behavior; otherwise return an intentional response or call context.next().
This is separate from Pages’ built-in routing: Pages maps URL paths to files in /functions, while mapping a hostname to a site or tenant is your application’s responsibility. The hostname must also be configured to reach the Pages project.
What subdomain routing means in Pages
Three separate mechanisms are involved, and each solves a different part of the problem:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
- DNS and custom domains direct a hostname such as
docs.example.comto the Pages project. - Middleware hostname inspection reads the incoming request URL and lets your code choose application behavior based on its hostname.
- Pages Function routing selects Functions by URL path and the project’s
/functionsdirectory structure, not by subdomain.
Cloudflare describes Pages routing as path-based, including dynamic path segments and fallback to static assets. It does not prescribe a universal host-to-tenant mapping. See Cloudflare Pages routing.
#1 Best Overall
Configure the hostname to reach the Pages project
Before middleware can distinguish a subdomain, configure that hostname as a custom domain for the Pages project and ensure its DNS directs traffic to the project. Cloudflare’s custom domains documentation explains the Pages setup. When the domain’s nameservers are not pointed to Cloudflare, its instructions describe using a custom CNAME record for a subdomain.
DNS establishes where a request goes; it does not decide which tenant’s data or site the application should serve. That decision belongs in the application.
Rank #2
Add root-level middleware to inspect the host
In the default Pages Functions system, create functions/_middleware.js at the project root when the host check must apply across the project. Cloudflare documents root middleware as applying across the application, including before static files. Middleware in a subdirectory has narrower scope: it applies to matching Functions in that directory and its descendants. See Cloudflare middleware.
An illustrative pattern is:
export async function onRequest(context) {
const url = new URL(context.request.url);
const hostname = url.hostname.toLowerCase();
// Map only hostnames configured for this application.
// Decide explicitly how unknown hosts should behave.
if (hostname === "docs.example.com") {
// Apply the docs site behavior.
}
return context.next();
}
This demonstrates the request and continuation pattern; it is not a complete or tested tenant implementation. Cloudflare provides the request through context.request, and context.next() continues to another applicable Function or the asset server. The middleware lifecycle and request context are documented in the middleware guide and Pages Functions API reference.
Choose explicit behavior for each hostname
For a known hostname, apply the site-selection behavior your application requires. For an unrecognized hostname, either return a response appropriate to the application or continue with context.next(). The choice is application-specific; Cloudflare’s documentation does not set a universal unknown-host response or security policy.
Do not treat an arbitrary incoming hostname as a trusted tenant identifier. If your code uses a hostname to select tenant data, map it through an explicit allowlist or a controlled lookup, and ensure that an unrecognized host cannot select unintended data.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Check which requests invoke Functions
Middleware runs only when the request reaches the Pages Functions system. Review the project’s _routes.json, including versions generated by a framework, to understand the paths that invoke Functions. Cloudflare documents that exclusions take priority over inclusions, so an excluded path will not be brought back into scope by a matching inclusion. See the routing documentation.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →This matters when expecting root middleware to inspect requests for static files: root placement gives middleware broad project scope, but the route configuration still determines which paths invoke Functions.
Choose between middleware and advanced mode
Use the default /functions system if you want Pages’ file-based path routing and middleware, with hostname checks added as application logic. Consider advanced mode when you need the Worker to control incoming requests more directly. The Worker pattern replaces the /functions directory system, so the two approaches have different routing and asset responsibilities.
| Approach | Routing control | Middleware and Functions | Static assets |
|---|---|---|---|
/functions with _middleware.js |
File-based path routes, plus application-defined hostname checks. | Uses Pages Functions and middleware. | context.next() can continue to another Function or the asset server; check _routes.json for invocation scope. |
Advanced mode with _worker.js |
The Worker controls incoming requests. | The /functions routing and middleware system is ignored. |
The Worker can serve static assets through the ASSETS binding. |
In advanced mode, preserve the intended static-asset behavior in the Worker; see Cloudflare’s advanced mode documentation. For the default system and setup, see Get started with Pages Functions.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

