Cloudflare Configuration Guide

This Documentation is based on the following official Cloudflare Docs: URL forwarding


1. Create the Cloudflare Worker

You will write a simple JavaScript function to intercept the request, modify the URL, and fetch the content from the new location.

Worker Code (JavaScript)

  1. Log in to your Cloudflare Dashboard and navigate to Workers & Pages.

  2. Click Create Application and then Create Worker. Give your worker a meaningful name (e.g., reverse-proxy-hash).

  3. Replace the default code with the following script:

// The primary domain where the Worker is running (e.g., "url.com")
const SOURCE_DOMAIN = "url.com";

// The path prefix to look for on the source domain (e.g., "/hash")
const PATH_PREFIX = "/hash"; 

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

async function handleRequest(request) {
  const url = new URL(request.url);

  // 1. Check if the request starts with the required path prefix
  if (url.pathname.startsWith(PATH_PREFIX)) {
    // 2. Extract the part of the path *after* the prefix.
    // e.g., if url.pathname is "/hash/some/path", newPathname will be "/some/path"
    const newPathname = url.pathname.substring(PATH_PREFIX.length);

    // 3. Extract the subdomain from the path prefix (optional, but good for dynamic routing)
    // The example uses "hash" for the subdomain, which is hardcoded in the target origin below.

    // 4. Construct the new URL for the proxy target (the origin server)
    // Target: hash.url.com + /some/path + ?queryparams
    const targetUrl = new URL(newPathname, `https://hash.${SOURCE_DOMAIN}`);
    targetUrl.search = url.search; // Preserve query parameters

    // 5. Create a new request to the target origin, using the original request method and headers
    const proxyRequest = new Request(targetUrl.toString(), {
      method: request.method,
      headers: request.headers,
      body: request.body,
      redirect: 'follow'
    });
    
    // 6. Set the Host header to match the origin server's expected hostname (hash.url.com)
    // This is often crucial for the origin server to handle the request correctly.
    proxyRequest.headers.set('Host', `hash.${SOURCE_DOMAIN}`);

    // 7. Fetch the content from the target URL and return the response
    return fetch(proxyRequest);
  }

  // If the path does not start with /hash, simply fetch the original URL content (for url.com)
  return fetch(request); 
}

2. Deploy the Worker and Set the Route

Once you have saved and deployed the worker code, you need to tell Cloudflare when to execute it. This is done by setting a Route.

Route Configuration

  1. Navigate back to your Workers & Pages dashboard and select your new Worker.

  2. Go to the Triggers tab.

  3. In the Routes section, click Add route.

  4. Configure the route:

    • Route: url.com/hash/* (Replace url.com with your actual domain).

      • The asterisk (*) is a wildcard that ensures the Worker triggers for any path under /hash/.

    • Zone: Select the zone for your url.com domain.

    • Worker: Select the Worker you just created (e.g., reverse-proxy-hash).

  5. Click Add route.

Last updated

Was this helpful?