N Next.js 中文
原文 ↗

重定向

为你的 Next.js 应用添加重定向。

更新于 2026-06-30Next.js 16.4.6

重定向允许你将传入的请求路径重定向到不同的目标路径。

要使用重定向,你可以在 next.config.js 中使用 redirects 键:

next.config.jsjs
module.exports = {
  redirects() {
    return [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ]
  },
}

redirects 可以定义为同步或异步函数。它应当返回(或解析为)一个对象数组,每个对象包含 source、destination 和 permanent 属性:

  • source 是传入的请求路径模式。
  • destination 是你想要路由到的路径。
  • permanent true 或 false - 如果为 true,将使用 308 状态码,这会指示客户端/搜索引擎永久缓存该重定向;如果为 false,将使用 307 状态码,这是临时的且不会被缓存。

Next.js 为什么使用 307 和 308? 传统上,302 用于临时重定向,301 用于永久重定向,但许多浏览器会将重定向的请求方法改为 GET,而不管原始方法是什么。例如,如果浏览器向 POST /v1/users 发起请求,返回状态码 302 和位置 /v2/users,那么后续请求可能是 GET /v2/users,而不是预期的 POST /v2/users。Next.js 使用 307 临时重定向和 308 永久重定向状态码,以明确保留所使用的请求方法。

  • basePath:false 或 undefined - 如果为 false,则匹配时不会包含 basePath,只能用于外部重定向。
  • locale:false 或 undefined - 匹配时是否不应包含 locale。
  • has 是一个由 has 对象 组成的数组,这些对象包含 type、key 和 value 属性。
  • missing 是一个由 missing 对象 组成的数组,这些对象包含 type、key 和 value 属性。

重定向的检查发生在文件系统之前,文件系统包括页面和 /public 文件。

使用 Pages Router 时,重定向不会应用于客户端路由(Link、router.push),除非存在 Proxy 且与路径匹配。

应用重定向时,请求中提供的任何查询值都会传递到重定向目标。例如,请参见以下重定向配置:

{
  source: '/old-blog/:path*',
  destination: '/blog/:path*',
  permanent: false
}

须知:

请记住在 source 和 destination 路径的路径参数中,冒号 : 前要包含正斜杠 /,否则该路径将被视为字面字符串,你可能会面临造成无限重定向的风险。

当请求 /old-blog/post-1?hello=world 时,客户端将被重定向到 /blog/post-1?hello=world。

路径匹配

允许进行路径匹配,例如 /old-blog/:slug 将匹配 /old-blog/first-post(不匹配嵌套路径):

next.config.jsjs
module.exports = {
  redirects() {
    return [
      {
        source: '/old-blog/:slug',
        destination: '/news/:slug', // Matched parameters can be used in the destination
        permanent: true,
      },
    ]
  },
}

模式 /old-blog/:slug 匹配 /old-blog/first-post 和 /old-blog/post-1,但不匹配 /old-blog/a/b(不匹配嵌套路径)。模式锚定在开头:/old-blog/:slug 不会匹配 /archive/old-blog/first-post。

你可以在参数上使用修饰符:*(零个或多个)、+(一个或多个)、?(零个或一个)。例如,/blog/:slug* 匹配 /blog、/blog/a 和 /blog/a/b/c。

在 path-to-regexp 文档中阅读更多详细信息。

通配符路径匹配

要匹配通配符路径,可以在参数后使用 *,例如 /blog/:slug* 将匹配 /blog/a/b/c/d/hello-world:

next.config.jsjs
module.exports = {
  redirects() {
    return [
      {
        source: '/blog/:slug*',
        destination: '/news/:slug*', // Matched parameters can be used in the destination
        permanent: true,
      },
    ]
  },
}

正则路径匹配

要匹配正则路径,可以将正则表达式包裹在参数后的括号中,例如 /post/:slug(\\d{1,}) 将匹配 /post/123,但不会匹配 /post/abc:

next.config.jsjs
module.exports = {
  redirects() {
    return [
      {
        source: '/post/:slug(\\d{1,})',
        destination: '/news/:slug', // Matched parameters can be used in the destination
        permanent: false,
      },
    ]
  },
}

以下字符 (、)、{、}、:、*、+、? 用于正则路径匹配,因此当在 source 中作为非特殊值使用时,必须在其前面添加 \\ 进行转义:

next.config.jsjs
module.exports = {
  redirects() {
    return [
      {
        // this will match `/english(default)/something` being requested
        source: '/english\\(default\\)/:slug',
        destination: '/en-us/:slug',
        permanent: false,
      },
    ]
  },
}

Header、Cookie 和 Query 匹配

如果只想在 header、cookie 或 query 值也匹配 has 字段或不匹配 missing 字段时才匹配重定向,可以使用该配置。source 和所有 has 项都必须匹配,且所有 missing 项都必须不匹配,重定向才会生效。

has 和 missing 项可以包含以下字段:

  • type:String - 必须是 header、cookie、host 或 query。
  • key:String - 所选类型中要匹配的键。
  • value:String 或 undefined - 要检查的值,如果为 undefined,则任何值都会匹配。可以使用类似正则的字符串来捕获值的特定部分,例如,如果为 first-second 使用值 first-(?<paramName>.*),则可以在目标中通过 :paramName 使用 second。
next.config.jsjs
module.exports = {
  redirects() {
    return [
      // if the header `x-redirect-me` is present,
      // this redirect will be applied
      {
        source: '/:path((?!another-page$).*)',
        has: [
          {
            type: 'header',
            key: 'x-redirect-me',
          },
        ],
        permanent: false,
        destination: '/another-page',
      },
      // if the header `x-do-not-redirect` is present,
      // this redirect will NOT be applied
      {
        source: '/:path((?!another-page$).*)',
        missing: [
          {
            type: 'header',
            key: 'x-do-not-redirect',
          },
        ],
        permanent: false,
        destination: '/another-page',
      },
      // if the source, query, and cookie are matched,
      // this redirect will be applied
      {
        source: '/specific/:path*',
        has: [
          {
            type: 'query',
            key: 'page',
            // the page value will not be available in the
            // destination since value is provided and doesn't
            // use a named capture group e.g. (?<page>home)
            value: 'home',
          },
          {
            type: 'cookie',
            key: 'authorized',
            value: 'true',
          },
        ],
        permanent: false,
        destination: '/another/:path*',
      },
      // if the header `x-authorized` is present and
      // contains a matching value, this redirect will be applied
      {
        source: '/',
        has: [
          {
            type: 'header',
            key: 'x-authorized',
            value: '(?<authorized>yes|true)',
          },
        ],
        permanent: false,
        destination: '/home?authorized=:authorized',
      },
      // if the host is `example.com`,
      // this redirect will be applied
      {
        source: '/:path((?!another-page$).*)',
        has: [
          {
            type: 'host',
            value: 'example.com',
          },
        ],
        permanent: false,
        destination: '/another-page',
      },
    ]
  },
}

带 basePath 支持的重定向

在重定向中使用 basePath 支持 时,每个 source 和 destination 都会自动添加 basePath 前缀,除非你在重定向中添加 basePath: false:

next.config.jsjs
module.exports = {
  basePath: '/docs',

  redirects() {
    return [
      {
        source: '/with-basePath', // automatically becomes /docs/with-basePath
        destination: '/another', // automatically becomes /docs/another
        permanent: false,
      },
      {
        // does not add /docs since basePath: false is set
        source: '/without-basePath',
        destination: 'https://example.com',
        basePath: false,
        permanent: false,
      },
    ]
  },
}

带 i18n 支持的重定向

在 App Router 中实现国际化重定向时,可以在 next.config.js 重定向中包含 locale,但仅限硬编码路径。

对于动态或按请求处理 locale 的情况,请使用动态路由段和 proxy,它可以根据用户的首选语言进行重定向。

next.config.jsjs
module.exports = {
  redirects() {
    return [
      {
        // Manually handle locale prefixes for App Router
        source: '/en/old-path',
        destination: '/en/new-path',
        permanent: false,
      },
      {
        // Redirect for all locales using a parameter
        source: '/:locale/old-path',
        destination: '/:locale/new-path',
        permanent: false,
      },
      {
        // Redirect from one locale to another
        source: '/de/old-path',
        destination: '/en/new-path',
        permanent: false,
      },
      {
        // Catch-all redirect for multiple locales
        source: '/:locale(en|fr|de)/:path*',
        destination: '/:locale/new-section/:path*',
        permanent: false,
      },
    ]
  },
}

在少数情况下,你可能需要为较旧的 HTTP 客户端分配自定义状态码才能正确重定向。在这些情况下,你可以使用 statusCode 属性而不是 permanent 属性,但不能同时使用两者。为确保 IE11 兼容性,会为 308 状态码自动添加 Refresh 响应头。

其他重定向

版本历史

版本 变更
v13.3.0 添加了 missing。
v10.2.0 已添加 has。
v9.5.0 已添加 redirects。