What is a rewrite in Next.js, and how is it different from a redirect?
Short answer
A rewrite serves content from a different internal path while keeping the original URL visible in the browser's address bar, unlike a redirect which actually changes the URL the visitor sees, making rewrites useful for things like proxying requests to another service without…
Advertisement
AModel answer
A rewrite serves content from a different internal path while keeping the original URL visible in the browser's address bar, unlike a redirect which actually changes the URL the visitor sees, making rewrites useful for things like proxying requests to another service without exposing that underlying path.
// next.config.js
module.exports = {
async rewrites() {
return [
{ source: '/api/proxy/:path*', destination: 'https://external-api.example.com/:path*' }
];
}
};Real-world example
A company proxies requests to an external analytics service through their own domain using a rewrite, keeping the external service's actual address hidden from visitors browsing their site.
Likely follow-ups
- Can rewrites point to an entirely external URL outside your own domain?
- Do search engines see the original URL or the rewritten destination?
Related topics