Rewrites

Examples

Version History

VersionChanges
v10.2.0has added.
v9.5.0Rewrites added.

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

Rewrites act as a URL proxy and mask the destination path, making it appear the user hasn’t changed their location on the site. In contrast, redirects will reroute to a new page and show the URL changes.

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 are applied to client-side routing, a <Link href="/about"> will have the rewrite applied in the above example.

rewrites is an async function that expects to return either an array or an object of arrays (see below) holding objects with source and destination properties:

  • source: String - is the incoming request path pattern.
  • destination: String is the path you want to route to.
  • basePath: false or undefined - if false the basePath won’t be included when matching, can be used for external rewrites only.
  • locale: false or undefined - whether the locale should not be included when matching.
  • has is an array of has objects with the type, key and value properties.

When the rewrites function returns an array, rewrites are applied after checking the filesystem (pages and /public files) and before dynamic routes. When the rewrites function returns an object of arrays with a specific shape, this behavior can be changed and more finely controlled, as of v10.1 of Next.js:

  1. module.exports = {
  2. async rewrites() {
  3. return {
  4. beforeFiles: [
  5. // These rewrites are checked after headers/redirects
  6. // and before all files including _next/public files which
  7. // allows overriding page files
  8. {
  9. source: '/some-page',
  10. destination: '/somewhere-else',
  11. has: [{ type: 'query', key: 'overrideMe' }],
  12. },
  13. ],
  14. afterFiles: [
  15. // These rewrites are checked after pages/public files
  16. // are checked but before dynamic routes
  17. {
  18. source: '/non-existent',
  19. destination: '/somewhere-else',
  20. },
  21. ],
  22. fallback: [
  23. // These rewrites are checked after both pages/public files
  24. // and dynamic routes are checked
  25. {
  26. source: '/:path*',
  27. destination: `https://my-old-site.com/:path*`,
  28. },
  29. ],
  30. }
  31. },
  32. }

Note: rewrites in beforeFiles do not check the filesystem/dynamic routes immediately after matching a source, they continue until all beforeFiles have been checked.

The order Next.js routes are checked is:

  1. headers are checked/applied
  2. redirects are checked/applied
  3. beforeFiles rewrites are checked/applied
  4. static files from the public directory, _next/static files, and non-dynamic pages are checked/served
  5. afterFiles rewrites are checked/applied, if one of these rewrites is matched we check dynamic routes/static files after each match
  6. fallback rewrites are checked/applied, these are applied before rendering the 404 page and after dynamic routes/all static assets have been checked. If you use fallback: true/‘blocking’ in getStaticPaths, the fallback rewrites defined in your next.config.js will not be run.

Rewrite parameters

When using parameters in a rewrite the parameters will be passed in the query by default when none of the parameters are used in the destination.

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/old-about/:path*',
  6. destination: '/about', // The :path parameter isn't used here so will be automatically passed in the query
  7. },
  8. ]
  9. },
  10. }

If a parameter is used in the destination none of the parameters will be automatically passed in the query.

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/docs/:path*',
  6. destination: '/:path*', // The :path parameter is used here so will not be automatically passed in the query
  7. },
  8. ]
  9. },
  10. }

You can still pass the parameters manually in the query if one is already used in the destination by specifying the query in the destination.

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. source: '/:first/:second',
  6. destination: '/:first?second=:second',
  7. // Since the :first parameter is used in the destination the :second parameter
  8. // will not automatically be added in the query although we can manually add it
  9. // as shown above
  10. },
  11. ]
  12. },
  13. }

Note: for static pages from the Automatic Static Optimization or prerendering params from rewrites will be parsed on the client after hydration and provided in the query.

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. }

The following characters (, ), {, }, :, *, +, ? are used for regex path matching, so when used in the source as non-special values they must be escaped by adding \\ before them:

  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. {
  5. // this will match `/english(default)/something` being requested
  6. source: '/english\\(default\\)/:slug',
  7. destination: '/en-us/:slug',
  8. },
  9. ]
  10. },
  11. }

To only match a rewrite when header, cookie, or query values also match the has field can be used. Both the source and all has items must match for the rewrite to be applied.

has items have the following fields:

  • type: String - must be either header, cookie, host, or query.
  • key: String - the key from the selected type to match against.
  • value: String or undefined - the value to check for, if undefined any value will match. A regex like string can be used to capture a specific part of the value, e.g. if the value first-(?<paramName>.*) is used for first-second then second will be usable in the destination with :paramName.
  1. module.exports = {
  2. async rewrites() {
  3. return [
  4. // if the header `x-rewrite-me` is present,
  5. // this rewrite will be applied
  6. {
  7. source: '/:path*',
  8. has: [
  9. {
  10. type: 'header',
  11. key: 'x-rewrite-me',
  12. },
  13. ],
  14. destination: '/another-page',
  15. },
  16. // if the source, query, and cookie are matched,
  17. // this rewrite will be applied
  18. {
  19. source: '/specific/:path*',
  20. has: [
  21. {
  22. type: 'query',
  23. key: 'page',
  24. // the page value will not be available in the
  25. // destination since value is provided and doesn't
  26. // use a named capture group e.g. (?<page>home)
  27. value: 'home',
  28. },
  29. {
  30. type: 'cookie',
  31. key: 'authorized',
  32. value: 'true',
  33. },
  34. ],
  35. destination: '/:path*/home',
  36. },
  37. // if the header `x-authorized` is present and
  38. // contains a matching value, this rewrite will be applied
  39. {
  40. source: '/:path*',
  41. has: [
  42. {
  43. type: 'header',
  44. key: 'x-authorized',
  45. value: '(?<authorized>yes|true)',
  46. },
  47. ],
  48. destination: '/home?authorized=:authorized',
  49. },
  50. // if the host is `example.com`,
  51. // this rewrite will be applied
  52. {
  53. source: '/:path*',
  54. has: [
  55. {
  56. type: 'host',
  57. value: 'example.com',
  58. },
  59. ],
  60. destination: '/another-page',
  61. },
  62. ]
  63. },
  64. }

Rewriting to an external URL

Examples

Rewrites allow you to rewrite to an external url. This is especially useful for incrementally adopting Next.js. The following is an example rewrite for redirecting the /blog route of your main app to an external site.

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

If you’re using trailingSlash: true, you also need to insert a trailing slash in the source parameter. If the destination server is also expecting a trailing slash it should be included in the destination parameter as well.

  1. module.exports = {
  2. trailingSlash: true,
  3. async rewrites() {
  4. return [
  5. {
  6. source: '/blog/',
  7. destination: 'https://example.com/blog/',
  8. },
  9. {
  10. source: '/blog/:path*/',
  11. destination: 'https://example.com/blog/:path*/',
  12. },
  13. ]
  14. },
  15. }

Incremental adoption of Next.js

You can also have Next.js fall back to proxying to an existing website after checking all Next.js routes.

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. fallback: [
  5. {
  6. source: '/:path*',
  7. destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
  8. },
  9. ],
  10. }
  11. },
  12. }

See additional information on incremental adoption in the docs here.

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 to /without-basePath since basePath: false is set
  11. // Note: this can not be used for internal rewrites e.g. `destination: '/another'`
  12. source: '/without-basePath',
  13. destination: 'https://example.com',
  14. basePath: false,
  15. },
  16. ]
  17. },
  18. }

Rewrites with i18n support

When leveraging i18n support with rewrites each source and destination is automatically prefixed to handle the configured locales unless you add locale: false to the rewrite. If locale: false is used you must prefix the source and destination with a locale for it to be matched correctly.

  1. module.exports = {
  2. i18n: {
  3. locales: ['en', 'fr', 'de'],
  4. defaultLocale: 'en',
  5. },
  6. async rewrites() {
  7. return [
  8. {
  9. source: '/with-locale', // automatically handles all locales
  10. destination: '/another', // automatically passes the locale on
  11. },
  12. {
  13. // does not handle locales automatically since locale: false is set
  14. source: '/nl/with-locale-manual',
  15. destination: '/nl/another',
  16. locale: false,
  17. },
  18. {
  19. // this matches '/' since `en` is the defaultLocale
  20. source: '/en',
  21. destination: '/en/another',
  22. locale: false,
  23. },
  24. {
  25. // it's possible to match all locales even when locale: false is set
  26. source: '/:locale/api-alias/:path*',
  27. destination: '/api/:path*',
  28. locale: false,
  29. },
  30. {
  31. // this gets converted to /(en|fr|de)/(.*) so will not match the top-level
  32. // `/` or `/fr` routes like /:path* would
  33. source: '/(.*)',
  34. destination: '/another',
  35. },
  36. ]
  37. },
  38. }