Prompt
How do I run redirects at the edge for a global website?
Latest observation
To run redirects at the edge for a global website, you typically want the redirect logic to happen on a CDN or edge platform close to the user, rather than on your origin server. That gives you faster responses, lower origin load, and more consistent behavior worldwide.
Common ways to do it
1. Use your CDN’s built-in redirects
Many CDN providers support redirect rules directly in their control plane.
Examples:
- Cloudflare: Bulk Redirects, Redirect Rules, Workers
- Akamai: Edge Redirector / Property Manager rules
- Fastly: Edge Dictionaries + VCL, or Compute
- AWS CloudFront: Functions or Lambda@Edge
- Netlify / Vercel: Redirect config files or edge routing features
Best for:
- Simple permanent or temporary redirects
- Large redirect maps
- Minimal custom logic
2. Use edge functions
If redirects depend on request data like:
- country / locale
- device type
- cookie / session
- path patterns
- A/B testing or experiment buckets
then use an edge function or edge worker.
Typical flow:
- Request arrives at nearest edge location
- Edge code checks redirect rules
- If matched, return
301,302,307, or308 - If not matched, proxy to origin
3. Store redirect rules in edge-friendly storage
For large-scale global redirect sets, don’t hardcode every rule in the function if the list is big or changes often.
Use:
- edge key-value store
- distributed config store
- CDN redirect table
- static redirect manifest generated during deployment
This lets you update redirects without redeploying code.
Recommended architecture
For a global website, a practical pattern is:
- CDN in front of everything
- Redirect logic at edge
- Origin only handles requests that are not redirected
Example decision order:
- Exact legacy URL match
- Locale/country redirect
- Canonical host/scheme redirect
- Other app routing to origin
Things to watch out for
Use the right HTTP status code
- 301: permanent redirect
- 302: temporary redirect
- 307: temporary, preserves method
- 308: permanent, preserves method
For SEO and site migrations, 301 or 308 are usually preferred.
Prevent redirect loops
Always check:
- current host
- current path
- whether the destination is already the same as the request
- trailing slash normalization
Keep redirects deterministic
At the edge, redirects should be:
- fast
- cacheable where possible
- consistent across regions
Handle localization carefully
If you redirect by country or language:
- respect user-selected locale cookies
- avoid overriding explicit URLs
- provide a way to switch back
- consider SEO hreflang/canonical tags
Test globally
Verify from multiple geographies:
- North America
- Europe
- Asia-Pacific
- mobile and desktop
- different protocols and hosts
Example logic
Pseudo-code:
if (request.host === "old.example.com") {
return redirect("https://www.example.com", 301);
}
if (request.path === "/pricing-old") {
return redirect("/pricing", 301);
}
if (country === "FR" && !hasLocaleCookie) {
return redirect("/fr" + request.path, 302);
}
return fetchOrigin();
When edge redirects are a good fit
Use edge redirects when:
- you need low latency globally
- redirects are based on location or request metadata
- you have a migration with many old URLs
- you want to reduce origin traffic
When not to use them
Avoid edge redirects if:
- logic depends on heavy backend data
- you need complex session state
- redirects are highly dynamic and personalized beyond edge capabilities
If you want, I can give you:
- a Cloudflare Worker example,
- an AWS CloudFront/Lambda@Edge example, or
- a platform-agnostic redirect architecture for your site.