Rewrites

This feature was introduced in Next.js 9.5 and up. If you’re using older versions of Next.js, please upgrade before trying it out.

Examples

Rewrites allow you to map an incoming request path to a different destination path.

Rewrites are only available on the Node.js environment and do not affect client-side routing.

Rewrites are not able to override public files or routes in the pages directory as these have higher priority than rewrites. For example, if you have pages/index.js you are not able to rewrite / to another location unless you rename the pages/index.js file.

To use rewrites you can use the rewrites key in next.config.js:

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/about',
  6. destination: '/',
  7. },
  8. ]
  9. },
  10. }

rewrites is an async function that expects an array to be returned holding objects with source and destination properties:

  • source is the incoming request path pattern.
  • destination is the path you want to route to.

Path Matching

Path matches are allowed, for example /blog/:slug will match /blog/hello-world (no nested paths):

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/blog/:slug',
  6. destination: '/news/:slug', // Matched parameters can be used in the destination
  7. },
  8. ]
  9. },
  10. }

Wildcard Path Matching

To match a wildcard path you can use * after a parameter, for example /blog/:slug* will match /blog/a/b/c/d/hello-world:

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/blog/:slug*',
  6. destination: '/news/:slug*', // Matched parameters can be used in the destination
  7. },
  8. ]
  9. },
  10. }

Regex Path Matching

To match a regex path you can wrap the regex in parenthesis after a parameter, for example /blog/:slug(\\d{1,}) will match /blog/123 but not /blog/abc:

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/old-blog/:post(\\d{1,})',
  6. destination: '/blog/:post', // Matched parameters can be used in the destination
  7. },
  8. ]
  9. },
  10. }

Rewriting to an external URL

Examples

Rewrites allow you to rewrite to an external url. This is especially useful for incrementally adopting Next.js.

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/blog/:slug',
  6. destination: 'https://example.com/blog/:slug', // Matched parameters can be used in the destination
  7. },
  8. ]
  9. },
  10. }

Incremental adoption of Next.js

You can also make Next.js check the application routes before falling back to proxying to the previous website.

This way you don’t have to change the rewrites configuration when migrating more pages to Next.js

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. // we need to define a no-op rewrite to trigger checking
  5. // all pages/static files before we attempt proxying
  6. {
  7. source: '/:path*',
  8. destination: '/:path*',
  9. },
  10. {
  11. source: '/:path*',
  12. destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
  13. },
  14. ]
  15. },
  16. }

Rewrites with basePath support

When leveraging basePath support with rewrites each source and destination is automatically prefixed with the basePath unless you add basePath: false to the rewrite:

  1. module.exports = {
  2. basePath: '/docs',
  3. async rewrites() {
  4. return [
  5. {
  6. source: '/with-basePath', // automatically becomes /docs/with-basePath
  7. destination: '/another', // automatically becomes /docs/another
  8. },
  9. {
  10. // does not add /docs since basePath: false is set
  11. source: '/without-basePath',
  12. destination: '/another',
  13. basePath: false,
  14. },
  15. ]
  16. },
  17. }