Edge SEO With Cloudflare Workers: Redirects, Headers and HTML Fixes

How to make SEO fixes at the CDN layer with Cloudflare Workers: redirects, headers and HTML changes with HTMLRewriter, with the rules that keep edge SEO safe and temporary.

Edge SEO means making SEO changes at the CDN layer, between your server and visitors, instead of in the website’s code. With Cloudflare Workers you can add redirects, set headers such as X-Robots-Tag, fix titles or canonical tags in the HTML with the HTMLRewriter API, and serve files like robots.txt, often in minutes when the platform or development queue makes code changes slow. Treat it as a temporary bridge: every change must apply equally to users and crawlers, and the underlying site should be fixed eventually.

When edge SEO makes sense

  • A platform that does not allow the change, such as a hosted CMS or an old system no one can deploy safely
  • An urgent problem, such as a staging noindex that reached production, while a proper release is prepared
  • Large redirect maps during a migration
  • Adding headers to files such as PDFs

Example 1: bulk redirects

For simple redirect lists, Cloudflare’s Bulk Redirects feature needs no code. For logic, a Worker can map paths:

const REDIRECTS = {
  "/old-services/": "/services/",
  "/blog/geo-guide/": "/geo-vs-seo/",
};

export default {
  async fetch(request) {
    const url = new URL(request.url);
    const target = REDIRECTS[url.pathname];
    if (target) {
      return Response.redirect(new URL(target, url.origin).toString(), 301);
    }
    return fetch(request);
  },
};

Example 2: fix a canonical tag in the HTML

The HTMLRewriter API edits HTML as it streams through the Worker:

export default {
  async fetch(request) {
    const response = await fetch(request);
    const type = response.headers.get("content-type") || "";
    if (!type.includes("text/html")) return response;

    const url = new URL(request.url);
    const canonical = `https://www.example.com${url.pathname}`;

    return new HTMLRewriter()
      .on('link[rel="canonical"]', {
        element(el) { el.setAttribute("href", canonical); },
      })
      .on('meta[name="robots"]', {
        element(el) {
          if ((el.getAttribute("content") || "").includes("noindex")) el.remove();
        },
      })
      .transform(response);
  },
};

This sets canonicals to the production host, useful when a platform outputs the wrong domain, and removes a stray noindex. Restrict such rules to the routes that need them.

Example 3: headers for non-HTML files

export default {
  async fetch(request) {
    const response = await fetch(request);
    if (new URL(request.url).pathname.endsWith(".pdf")) {
      const headers = new Headers(response.headers);
      headers.set("X-Robots-Tag", "noindex");
      return new Response(response.body, { status: response.status, headers });
    }
    return response;
  },
};

The rules that keep edge SEO safe

  • Never serve different content to crawlers than to users. Google’s spam policies treat that as cloaking. Apply every change to all visitors.
  • Keep a written list of every edge change, with the reason and an owner, because future developers will not see it in the site’s code.
  • Test carefully: a mistake at the edge affects the whole site instantly. Deploy to a test route or a staging hostname first.
  • Watch caching: make sure modified responses are cached correctly, or not at all where content varies.
  • Plan to move each fix into the site itself, then remove it from the edge.

Checking the result

  1. curl the affected URLs and confirm status codes, headers and HTML.
  2. Run our SEO smoke test against production.
  3. Use Search Console’s URL Inspection to see what Google received.
  4. Check that AI crawlers get the same result with our AI crawler access checker.

Frequently asked questions

Is edge SEO against Google’s guidelines?

No, as long as users and crawlers receive the same content. Changing HTML at the edge for everyone is like changing it on the server.

Does it work with other CDNs?

Yes. Other CDNs offer edge functions with similar capabilities; the principles are the same.

What does it cost?

Cloudflare Workers has a free tier with a daily request limit and paid plans for more traffic. Check Cloudflare’s current pricing for your volume.

Sources

  1. Cloudflare Workers documentation, fetch handler and HTMLRewriter
  2. Cloudflare documentation, Bulk Redirects
  3. Google Search Central, “Spam policies for Google web search” (cloaking)