Cloudflare Workers Edge SEO 301 Redirect & Header Generator.
Generate a zero-latency ES Module Cloudflare Worker with O(1) Map-based 301 redirects and automatic staging X-Robots-Tag protection.
Edge redirect rules3
const REDIRECT_MAP = new Map([
["/old-services/seo", "/services/seo-agency"],
["/blog/2022-guide", "/guides/seo-playbook"],
["/contact-old", "/contact"]
]);
export default {
async fetch(request) {
const url = new URL(request.url);
const target = REDIRECT_MAP.get(url.pathname);
if (target) {
const destination = target.startsWith("http") ? target : new URL(target, url.origin).toString();
return Response.redirect(destination, 301);
}
const response = await fetch(request);
if (url.hostname.toLowerCase() === "staging.yourwebsite.co.il") {
const headers = new Headers(response.headers);
headers.set("X-Robots-Tag", "noindex, nofollow");
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
}
return response;
}
};How to use this in your SEO workflow
- Executing 301 redirects at the CDN edge eliminates origin round-trips and keeps Googlebot TTFB ultra-low.
- Using a JavaScript Map avoids expensive sequential regex evaluation across hundreds of legacy URLs.
- Automatically attach X-Robots-Tag: noindex on staging hostnames so preview deployments never leak into Google.
Related in-depth SEO guides
Strategy
Map Every Traffic-Bearing Old Address Before Launch Day
Build a complete old-URL inventory from Search Console, Backlinks and Top Pages so zero organic equity gets stranded on launch day.
Data readingResolve Ambiguous and Shared Destinations Manually
Why Serpo leaves ambiguous slugs unmatched instead of guessing—and how to review shared destinations safely.