Redirects

Examples

Version History

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

Redirects allow you to redirect an incoming request path to a different destination path.

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

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

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

  • source is the incoming request path pattern.
  • destination is the path you want to route to.
  • permanent true or false - if true will use the 308 status code which instructs clients/search engines to cache the redirect forever, if false will use the 307 status code which is temporary and is not cached.

Why does Next.js use 307 and 308? Traditionally a 302 was used for a temporary redirect, and a 301 for a permanent redirect, but many browsers changed the request method of the redirect to GET, regardless of the original method. For example, if the browser made a request to POST /v1/users which returned status code 302 with location /v2/users, the subsequent request might be GET /v2/users instead of the expected POST /v2/users. Next.js uses the 307 temporary redirect, and 308 permanent redirect status codes to explicitly preserve the request method used.

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

Redirects are checked before the filesystem which includes pages and /public files.

When a redirect is applied, any query values provided in the request will be passed through to the redirect destination. For example, see the following redirect configuration:

  1. {
  2. source: '/old-blog/:path*',
  3. destination: '/blog/:path*',
  4. permanent: false
  5. }

When /old-blog/post-1?hello=world is requested, the client will be redirected to /blog/post-1?hello=world.

Path Matching

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

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

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 redirects() {
  3. return [
  4. {
  5. source: '/blog/:slug*',
  6. destination: '/news/:slug*', // Matched parameters can be used in the destination
  7. permanent: true,
  8. },
  9. ]
  10. },
  11. }

Regex Path Matching

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

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

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 redirects() {
  3. return [
  4. {
  5. // this will match `/english(default)/something` being requested
  6. source: '/english\\(default\\)/:slug',
  7. destination: '/en-us/:slug',
  8. permanent: false,
  9. },
  10. ]
  11. },
  12. }

To only match a redirect 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 redirect 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 redirects() {
  3. return [
  4. // if the header `x-redirect-me` is present,
  5. // this redirect will be applied
  6. {
  7. source: '/:path((?!another-page$).*)',
  8. has: [
  9. {
  10. type: 'header',
  11. key: 'x-redirect-me',
  12. },
  13. ],
  14. permanent: false,
  15. destination: '/another-page',
  16. },
  17. // if the source, query, and cookie are matched,
  18. // this redirect will be applied
  19. {
  20. source: '/specific/:path*',
  21. has: [
  22. {
  23. type: 'query',
  24. key: 'page',
  25. // the page value will not be available in the
  26. // destination since value is provided and doesn't
  27. // use a named capture group e.g. (?<page>home)
  28. value: 'home',
  29. },
  30. {
  31. type: 'cookie',
  32. key: 'authorized',
  33. value: 'true',
  34. },
  35. ],
  36. permanent: false,
  37. destination: '/another/:path*',
  38. },
  39. // if the header `x-authorized` is present and
  40. // contains a matching value, this redirect will be applied
  41. {
  42. source: '/',
  43. has: [
  44. {
  45. type: 'header',
  46. key: 'x-authorized',
  47. value: '(?<authorized>yes|true)',
  48. },
  49. ],
  50. permanent: false,
  51. destination: '/home?authorized=:authorized',
  52. },
  53. // if the host is `example.com`,
  54. // this redirect will be applied
  55. {
  56. source: '/:path((?!another-page$).*)',
  57. has: [
  58. {
  59. type: 'host',
  60. value: 'example.com',
  61. },
  62. ],
  63. permanent: false,
  64. destination: '/another-page',
  65. },
  66. ]
  67. },
  68. }

Redirects with basePath support

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

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

Redirects with i18n support

When leveraging i18n support with redirects each source and destination is automatically prefixed to handle the configured locales unless you add locale: false to the redirect. 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 redirects() {
  7. return [
  8. {
  9. source: '/with-locale', // automatically handles all locales
  10. destination: '/another', // automatically passes the locale on
  11. permanent: false,
  12. },
  13. {
  14. // does not handle locales automatically since locale: false is set
  15. source: '/nl/with-locale-manual',
  16. destination: '/nl/another',
  17. locale: false,
  18. permanent: false,
  19. },
  20. {
  21. // this matches '/' since `en` is the defaultLocale
  22. source: '/en',
  23. destination: '/en/another',
  24. locale: false,
  25. permanent: false,
  26. },
  27. // it's possible to match all locales even when locale: false is set
  28. {
  29. source: '/:locale/page',
  30. destination: '/en/newpage',
  31. permanent: false,
  32. locale: false,
  33. }
  34. {
  35. // this gets converted to /(en|fr|de)/(.*) so will not match the top-level
  36. // `/` or `/fr` routes like /:path* would
  37. source: '/(.*)',
  38. destination: '/another',
  39. permanent: false,
  40. },
  41. ]
  42. },
  43. }

In some rare cases, you might need to assign a custom status code for older HTTP Clients to properly redirect. In these cases, you can use the statusCode property instead of the permanent property, but not both. Note: to ensure IE11 compatibility a Refresh header is automatically added for the 308 status code.

Other Redirects