Skip to main content

Implementing URL Rewrites

URL rewrites at the edge let you serve Files.com Public Hosting content through your own domain without redirecting users or exposing the original hosted-by-files.com URLs. Each major edge platform handles request rewriting differently. The sections below cover Cloudflare, Fastly, Akamai, AWS, Vercel, and Netlify. Review the provider's documentation for configuration details and best practices.

Cloudflare Workers

Cloudflare Workers run URL rewrite rules at the edge, so you can route requests cleanly without exposing the underlying hosted-by-files.com URL. To serve your Publicly Hosted Folder from your own domain, such as example.com/resources, use a Worker to intercept requests and forward them to your Files.com-hosted content.

To set this up, log in to your Cloudflare account and select the example.com domain. In Cloudflare's left navigation, go to Workers & Pages and open the Manage Workers tab. Create a new WorkerExternal LinkThis link leads to an external website and will open in a new tab to launch a serverless function. Cloudflare provides an online editor with starter code, which you can replace with a custom scriptExternal LinkThis link leads to an external website and will open in a new tab that rewrites the path and routes it to your Public Hosting URL on Files.com.

For example, if your Public Hosting folder is available at https://your-subdomain.hosted-by-files.com/resources/ and you want users to access it through https://example.com/resources/*, use the following Worker script.

export default {
  async fetch(request) {
    const originalUrl = new URL(request.url);
    const rewrittenPath = originalUrl.pathname.replace(/^\/resources/, '');
    const newUrl = `https://your-subdomain.hosted-by-files.com/resources${rewrittenPath}${originalUrl.search}`;

    const response = await fetch(newUrl, request);
    const contentType = response.headers.get("content-type") || "";

    if (contentType.includes("text/html")) {
      return new HTMLRewriter().transform(response);
    }

    return response;
  }
}

After deploying the Worker, open the Triggers section of the Worker configuration and define a routeExternal LinkThis link leads to an external website and will open in a new tab that matches example.com/resources/*. Assign the route to the correct zone, which refers to the domain you manage within Cloudflare. In this case, the zone is example.com. The zone must already exist in your Cloudflare account, and your DNS for example.com must be proxied through Cloudflare for the route to take effect.

With this setup, Cloudflare runs the Worker whenever a user visits any path under /resources on your domain. When a user accesses a URL like example.com/resources/sample.pdf, the Worker rewrites the request and fetches the file from your Files.com Public Hosting folder. The first request retrieves the file from hosted-by-files.com, and Cloudflare caches it at the edge for future requests. This improves performance and reduces outbound usage on your Files.com site. Users interact only with your branded domain and never see the underlying hosted-by-files.com address.

Handling Mismatched Paths Between Files.com and Your Domain

If the URL path on Files.com matches the route used in Cloudflare (for example, both use /resources/), then both direct file access such as https://example.com/resources/sample.pdf and folder browsing through the index page work as expected without additional logic. Index pages display when you enable the Display index page when viewing folders option on the publicly hosted folder in Files.com.

If the URL path on Files.com differs from the route exposed on your domain (for example, Files.com uses /myfolder/ while your public route is /resources/), the default index page generated by Files.com contains absolute links that won't resolve correctly under your domain. These mismatched links break folder navigation and prevent file downloads from the index view.

To address this, either update the Public Hosting URL on Files.com to match your route, or upload a custom index.html file with the correct internal paths. You can also modify your Worker to rewrite internal linksExternal LinkThis link leads to an external website and will open in a new tab in the index HTML response using HTMLRewriter. Add the following snippet to your Worker to rewrite links:

.on("a", {
  element(el) {
    const href = el.getAttribute("href");
    if (href?.startsWith("/myfolder/")) {
      el.setAttribute("href", href.replace("/myfolder/", "/resources/"));
    }
  }
})

With this in place, users can navigate through folders and download files from the index page even when the path in your Files.com URL differs from the one exposed on your site.

Fastly Compute@Edge

Fastly's Compute@Edge runs custom logic at the edge for dynamic URL path rewritingExternal LinkThis link leads to an external website and will open in a new tab and request routing.

Akamai EdgeWorkers

Akamai's EdgeWorkers manipulate HTML content dynamically at the edge for URL rewriting and content modificationExternal LinkThis link leads to an external website and will open in a new tab before content reaches the client.

AWS Lambda@Edge

AWS Lambda@Edge runs functionsExternal LinkThis link leads to an external website and will open in a new tab such as dynamic URL rewriting and request handling at AWS edge locations.

Vercel Edge Middleware

Vercel's Edge Middleware intercepts and modifies requests at the edge, supporting hostname rewrites and dynamic routingExternal LinkThis link leads to an external website and will open in a new tab for your applications.

Netlify Edge Functions

Netlify Edge Functions rewrite requests on one URL to resources available on another URLExternal LinkThis link leads to an external website and will open in a new tab for flexible request handling and routing at the edge.