Skip to content

Commit 9aabde0

Browse files
authored
Subdirectory routing for Webflow + Cloudflare (#793)
* add info on routing for webflow * change staging domain to landing page * staging -> landing
1 parent 5c3a627 commit 9aabde0

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

advanced/subpath/cloudflare.mdx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,56 @@ If your domain already points to another service, you must remove the existing D
8585

8686
1. Delete the existing DNS record for your domain. See [Delete DNS records](https://developers.cloudflare.com/dns/manage-dns-records/how-to/create-dns-records/#delete-dns-records) in the Cloudflare documentation for more information.
8787
2. Return to your Worker and add your custom domain.
88+
89+
## Webflow custom routing
90+
If you use Webflow to host your main site and want to serve Mintlify docs at `/docs` on the same domain, you'll need to configure custom routing through Cloudflare Workers to proxy all non-docs traffic to your main site.
91+
92+
<Warning>
93+
Make sure your main site is set up on a landing page before deploying this Worker, or visitors to your main site will see errors.
94+
</Warning>
95+
96+
1. In Webflow, set up a landing page for your main site like `landing.yoursite.com`. This will be the page that visitors see when they visit your site.
97+
2. Deploy your main site to the landing page. This ensures that your main site remains accessible while you configure the Worker.
98+
3. To avoid conflicts, update any absolute URLs in your main site to be relative.
99+
4. In Cloudflare, select **Edit Code** and add the following script into your Worker's code.
100+
101+
<Tip> Replace `[SUBDOMAIN]` with your unique subdomain, `[YOUR_DOMAIN]` with your website's base URL, and `[LANDING_DOMAIN]` with your landing page URL. </Tip>
102+
103+
```javascript
104+
addEventListener("fetch", (event) => {
105+
event.respondWith(handleRequest(event.request));
106+
});
107+
async function handleRequest(request) {
108+
try {
109+
const urlObject = new URL(request.url);
110+
// If the request is to the docs subdirectory
111+
if (/^\/docs/.test(urlObject.pathname)) {
112+
// Proxy to Mintlify
113+
const DOCS_URL = "[SUBDOMAIN].mintlify.dev";
114+
const CUSTOM_URL = "[YOUR_DOMAIN]";
115+
let url = new URL(request.url);
116+
url.hostname = DOCS_URL;
117+
let proxyRequest = new Request(url, request);
118+
proxyRequest.headers.set("Host", DOCS_URL);
119+
proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);
120+
proxyRequest.headers.set("X-Forwarded-Proto", "https");
121+
return await fetch(proxyRequest);
122+
}
123+
// Route everything else to main site
124+
const MAIN_SITE_URL = "[LANDING_DOMAIN]";
125+
if (MAIN_SITE_URL && MAIN_SITE_URL !== "[LANDING_DOMAIN]") {
126+
let mainSiteUrl = new URL(request.url);
127+
mainSiteUrl.hostname = MAIN_SITE_URL;
128+
return await fetch(mainSiteUrl, {
129+
method: request.method,
130+
headers: request.headers,
131+
body: request.body
132+
});
133+
}
134+
} catch (error) {
135+
// If no action found, serve the regular request
136+
return await fetch(request);
137+
}
138+
}
139+
```
140+
5. Select Deploy and wait for the changes to propagate, which can take up to a few hours.

0 commit comments

Comments
 (0)