Prompt
How do I handle redirects and header changes at the edge?
Latest observation
Oct 26, 2026
OpenAI APIWeb search: off
Handling redirects and header changes at the edge usually means using a CDN, reverse proxy, or edge platform to make routing decisions before traffic reaches your origin.
Common patterns
1) Redirects
Use edge logic when you want to:
- move old URLs to new ones
- enforce HTTPS
- normalize hostnames
- do geo/device-based routing
Typical redirect types:
- 301: permanent
- 302: temporary
- 307/308: preserve HTTP method and body
2) Header manipulation
At the edge you can:
- add security headers
- rewrite
Host - set caching headers
- strip sensitive headers
- inject geo/user-agent info into upstream requests
- control CORS behavior
Where this is done
Depends on your platform:
- CDNs: Cloudflare, Fastly, Akamai, CloudFront
- Reverse proxies: Nginx, Envoy, HAProxy
- Edge runtimes: Cloudflare Workers, Vercel Edge Functions, Netlify Edge Functions
Examples
Nginx
server {
listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Request-Id $request_id;
add_header X-Frame-Options DENY;
}
Cloudflare Worker
export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/old") {
return Response.redirect("https://example.com/new", 301);
}
const headers = new Headers(request.headers);
headers.set("X-Edge-Added", "true");
headers.delete("Authorization");
return fetch(new Request(request, { headers }));
}
}
Best practices
- Prefer 301/308 for permanent moves
- Avoid redirect chains
- Keep header changes minimal and explicit
- Preserve
X-Forwarded-For,X-Forwarded-Proto, and request IDs - Test cache behavior carefully
- Make sure security headers aren’t overwritten later by origin responses
Key question
The exact method depends on your edge platform. If you tell me which one you’re using, I can give you a precise config or code example.