Redirects and Rewrites
Configure URL redirects and rewrites at the edge for SEO and migration.
EdgeBeginner
Why
Edge redirects preserve SEO link equity and handle domain migrations without origin round-trips. Cloudflare's edge network spans 330+ cities with sub-50ms p95 latency globally.
Edge functions reduce TTFB by 60-80% compared to origin servers, and Akamai reports 40% of enterprises now use edge compute in 2024.
When
Domain migration, URL restructuring, or legacy URL support. Avoid edge functions for stateful logic requiring database writes — edge runtimes have no persistent connection pool.
Skip if you need long-running tasks (>30s) or heavy CPU computation.
How
# next.config.js redirects and rewrites
module.exports = {
async redirects() {
return [
{ source: '/old-blog/:slug', destination: '/blog/:slug', permanent: true },
{ source: '/docs', destination: '/documentation', permanent: false },
];
},
async rewrites() {
return [
{ source: '/api/v1/:path*', destination: 'https://api.example.com/v1/:path*' },
{ source: '/proxy/:path*', destination: '/api/:path*' },
];
},
};
4. Verify deployment — curl -I https://your-app.example.com
5. Monitor logs — check for errors in first 5 minutes
6. Rollback if needed — revert to previous version
Example
# vercel.json redirects
{
"redirects": [
{ "source": "/old", "destination": "/new", "permanent": true },
{ "source": "/external", "destination": "https://example.com", "permanent": false }
],
"rewrites": [
{ "source": "/(.*)", "destination": "/index.html" }
]
}
Common Problems
- ⚠Redirect loop — check source/destination don't overlap
- ⚠Rewrite not working — verify destination is reachable
- ⚠Configuration drift between environments — use infrastructure-as-code (Terraform/Pulumi) to keep dev/staging/prod identical
Best Practices
- ✓Use 301 for permanent moves — preserves SEO
- ✓Use rewrites for API proxying — avoid CORS issues
- ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit