Prerequisite: Your primary domain (company.com) is hosted on and you are on the Mintlify Pro plan or above.

Setup Steps

  1. Create a new Cloudflare Worker at Workers & Pages > Create application > Create worker
  1. Add your custom domain by going to Settings > Triggers > Add Custom Domain
    • Add both domain.com and www.domain.com versions
  1. Click Edit Code and paste the following script:

Replace [SUBDOMAIN] and [YOUR_DOMAIN] in DOCS_URL and CUSTOM_URL with your values

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  try {
    const urlObject = new URL(request.url);
    // If the request is to the docs subdirectory
    if (/^\/docs/.test(urlObject.pathname)) {
      // Then Proxy to Mintlify
      const DOCS_URL = "[SUBDOMAIN].mintlify.dev";
      const CUSTOM_URL = "[YOUR_DOMAIN]";

      let url = new URL(request.url);
      url.hostname = DOCS_URL;

      let proxyRequest = new Request(url, request);

      proxyRequest.headers.set("Host", DOCS_URL);
      proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);
      proxyRequest.headers.set("X-Forwarded-Proto", "https");

      return await fetch(proxyRequest);
    }
  } catch (error) {
    // if no action found, play the regular request
    return await fetch(request);
  }
}
  1. Click Deploy (changes may take a few hours to propagate)

Need help? Contact our support team

Was this page helpful?