Cloudflare Workers Subpath Setup
Host documentation at a custom subpath using Cloudflare Workers for flexible reverse proxy configuration.
Overview
Use Cloudflare Workers to host your documentation at a custom subpath like yoursite.com/docs. Workers act as a reverse proxy, routing documentation requests to your Documentation.AI hosting while keeping your main site intact.
Requires: Cloudflare account, domain on Cloudflare, documentation at [SUBDOMAIN].documentationai.io
How It Works
The Worker intercepts all requests to your domain:
- Requests to
/docs(or your chosen subpath) → Proxied to Documentation.AI - All other requests → Passed through to your main site
Step 1: Create Worker
Open Cloudflare Dashboard
Go to Cloudflare Dashboard → Workers & Pages.
Create New Worker
Click Create application → Create Worker → Deploy.
Name Your Worker
Give it a name like docs-proxy and click Deploy.
Step 2: Configure Worker Script
Edit Worker Code
Click Edit Code to open the code editor.
Replace Default Code
Delete all existing code and paste the script below.
Update Configuration
Replace the placeholders with your actual values (see callout below).
Replace: [SUBDOMAIN] with your subdomain, [YOUR_DOMAIN] with your domain, /docs with your path.
Worker Script
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const url = new URL(request.url);
// Allow SSL verification paths to pass through
if (url.pathname.startsWith('/.well-known/')) {
return await fetch(request);
}
// Route documentation requests to Documentation.AI
// Change /docs to your chosen subpath (e.g., /help, /guide, /api-docs)
if (url.pathname.startsWith('/docs')) {
const DOCS_HOST = "[SUBDOMAIN].documentationai.io";
const YOUR_DOMAIN = "[YOUR_DOMAIN]";
// Update the URL to point to documentation host
url.hostname = DOCS_HOST;
// Create proxy request with proper headers
const proxyRequest = new Request(url, request);
proxyRequest.headers.set("Host", DOCS_HOST);
proxyRequest.headers.set("X-Forwarded-Host", YOUR_DOMAIN);
proxyRequest.headers.set("X-Forwarded-Proto", "https");
return await fetch(proxyRequest);
}
// Pass all other requests to your main site
return await fetch(request);
} catch (error) {
return new Response("Error: " + error.message, { status: 500 });
}
}
Change /docs on line 17 to your path (e.g., /help, /api-docs).
Step 3: Deploy Worker
Save and Deploy
Click Save and Deploy in the top-right corner.
Test Worker URL
Copy your Worker URL (shown as your-worker.your-subdomain.workers.dev).
Test Documentation
Visit your-worker.your-subdomain.workers.dev/docs to verify it loads your documentation.
Step 4: Connect Your Domain
Add Custom Domain
In the Worker page, go to Settings → Triggers → Add Custom Domain.
Enter Your Domain
Enter your domain (e.g., yoursite.com) and click Add Custom Domain.
Add WWW Subdomain
Repeat to add www.yoursite.com if you use the www subdomain.
Step 5: Test Your Setup
Test Documentation
Visit yoursite.com/docs (or your chosen subpath) - should show documentation.
Test Main Site
Visit yoursite.com - should show your main site.
Test Nested Pages
Visit yoursite.com/docs/any-page - should show documentation pages.
Check Browser Console
Open DevTools → Console - should have no errors.
✅ Your documentation is now live at your custom subpath!
Advanced: Webflow Integration
If your main site is hosted on Webflow, you need to route non-docs traffic to your Webflow landing page. Add this configuration to your Worker script:
// Add after line 27 (after the /docs routing)
// Route all other traffic to Webflow landing page
const WEBFLOW_LANDING = "landing.yoursite.webflow.io";
const mainSiteUrl = new URL(request.url);
mainSiteUrl.hostname = WEBFLOW_LANDING;
return await fetch(mainSiteUrl, {
method: request.method,
headers: request.headers,
body: request.body
});
Replace landing.yoursite.webflow.io with your actual Webflow landing page domain.
Troubleshooting
Configuration Reference
Key Headers
| Header | Value | Purpose |
|---|---|---|
Host | [SUBDOMAIN].documentationai.io | Tell documentation host which site to serve |
X-Forwarded-Host | yoursite.com | Preserve original domain for analytics |
X-Forwarded-Proto | https | Ensure secure connections |
Path Handling
| Path | Action | Destination |
|---|---|---|
/.well-known/* | Pass through | Original request (for SSL verification) |
/docs/* | Proxy | Documentation.AI hosting |
| All others | Pass through | Your main site |
Next Steps
- Monitor Worker performance in Cloudflare Dashboard
- Set up analytics to track documentation traffic
- Configure custom error pages if needed
- Test from different locations to verify global performance
Last updated today