Consolidate two domains with one Cloudflare Worker.
You have a legacy site on one host and a shiny new one on another. Marketing wants
everything under www.example.com. SEO won't survive a hard cutover.
A reverse proxy in front of both origins, deployed as a Cloudflare Worker, fixes this
in about 80 lines of JavaScript. This walkthrough builds one with you.
What you'll have when you're done
- A single Worker bound to
www.example.com/*. - Path-based routing to a primary and secondary origin.
- Response rewriting so
canonical,hreflang, andog:urlalways point at your canonical host. - Sensible defaults for headers, CSP, and redirects.
- A
wrangler.tomlready fornpm run deploy.
Steps
- The problem Two domains, one brand. Why a reverse proxy beats a 301.
- Origins & routing Pick a canonical host, define origins, route by path.
- URL rewrites Rewrite canonical, hreflang, og:url, and JSON-LD back to the canonical host.
- Headers & CSP What to strip, what to forward, and how CSP breaks proxies.
- Redirects When to 301 instead of proxy, and how to keep the canonical host.
- Deploy wrangler.toml routes, custom domain, robots.txt pointer.
Each step ships with a live request simulator: change the rules, paste a URL,
and see exactly what the Worker would do. No network calls — the same logic runs in your browser.