redirects
重定向允许您将传入请求路径重定向到不同的目标路径。
要使用重定向,您可以在 next.config.js 中使用 redirects 键。
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: true,
},
]
},
}redirects 是一个异步函数,它需要返回一个数组,其中包含带有 source、destination 和 permanent 属性的对象。
source是传入请求的路径模式。destination是您要路由到的路径。permanenttrue或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是一个具有type、key和value属性的 has 对象数组。missing是一个具有type、key和value属性的 missing 对象数组。
重定向在文件系统(包括页面和 /public 文件)之前进行检查。
使用 Pages Router 时,重定向不适用于客户端路由(Link、router.push),除非存在并匹配路径的 代理。
应用重定向时,请求中提供的所有查询值都将传递给重定向目标。例如,请参阅以下重定向配置。
{
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/hello-world(无嵌套路径)。
module.exports = {
async redirects() {
return [
{
source: '/old-blog/:slug',
destination: '/news/:slug', // Matched parameters can be used in the destination
permanent: true,
},
]
},
}通配符路径匹配
要匹配通配符路径,您可以在参数后使用 *,例如 /blog/:slug* 将匹配 /blog/a/b/c/d/hello-world
module.exports = {
async 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。
module.exports = {
async redirects() {
return [
{
source: '/post/:slug(\\d{1,})',
destination: '/news/:slug', // Matched parameters can be used in the destination
permanent: false,
},
]
},
}以下字符 (、)、{、}、:、*、+、? 用于正则表达式路径匹配,因此当在 source 中用作非特殊值时,必须通过在它们前面添加 \\ 进行转义
module.exports = {
async redirects() {
return [
{
// this will match `/english(default)/something` being requested
source: '/english\\(default\\)/:slug',
destination: '/en-us/:slug',
permanent: false,
},
]
},
}标头、Cookie 和查询匹配
仅当 header、cookie 或查询值也匹配 has 字段或不匹配 missing 字段时,才可以使用 has 字段或 missing 字段来匹配重定向。source 和所有 has 项都必须匹配,并且所有 missing 项都必须不匹配,才能应用重定向。
has 和 missing 项可以具有以下字段
type:String- 必须是header、cookie、host或query。key:String- 要匹配的所选类型的键。value:String或undefined- 要检查的值,如果未定义,则任何值都将匹配。可以使用类似正则表达式的字符串来捕获值的特定部分,例如,如果first-(?<paramName>.*)用于first-second,那么second将在目标中与:paramName一起使用。
module.exports = {
async 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-dont-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 支持 进行重定向时,除非您将 basePath: false 添加到重定向中,否则每个 source 和 destination 都会自动添加 basePath 前缀。
module.exports = {
basePath: '/docs',
async 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 的重定向
当利用 i18n 支持 进行重定向时,除非您将 locale: false 添加到重定向中,否则每个 source 和 destination 都会自动添加前缀以处理配置的 locales。如果使用 locale: false,您必须为 source 和 destination 添加区域设置前缀,才能正确匹配。
module.exports = {
i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
},
async redirects() {
return [
{
source: '/with-locale', // automatically handles all locales
destination: '/another', // automatically passes the locale on
permanent: false,
},
{
// does not handle locales automatically since locale: false is set
source: '/nl/with-locale-manual',
destination: '/nl/another',
locale: false,
permanent: false,
},
{
// this matches '/' since `en` is the defaultLocale
source: '/en',
destination: '/en/another',
locale: false,
permanent: false,
},
// it's possible to match all locales even when locale: false is set
{
source: '/:locale/page',
destination: '/en/newpage',
permanent: false,
locale: false,
},
{
// this gets converted to /(en|fr|de)/(.*) so will not match the top-level
// `/` or `/fr` routes like /:path* would
source: '/(.*)',
destination: '/another',
permanent: false,
},
]
},
}在某些罕见情况下,您可能需要为旧版 HTTP 客户端分配自定义状态码才能正确重定向。在这些情况下,您可以使用 statusCode 属性而不是 permanent 属性,但不能同时使用两者。为确保 IE11 兼容性,对于 308 状态码,会自动添加 Refresh 头部。
其他重定向
- 在 API 路由 和 路由处理程序 内部,您可以根据传入请求进行重定向。
- 在
getStaticProps和getServerSideProps内部,您可以在请求时重定向特定页面。
版本历史
| 版本 | 更改 |
|---|---|
v13.3.0 | missing 已添加。 |
v10.2.0 | has 已添加。 |
v9.5.0 | 已添加 redirects。 |
这有帮助吗?