redirects
重新導向允許您將傳入的請求路徑重新導向到不同的目標路徑。
若要使用重新導向,您可以使用 next.config.js
中的 redirects
鍵
module.exports = {
async 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
- 是否不應在比對時包含語系。has
是一個 has 物件 陣列,具有type
、key
和value
屬性。missing
是一個 missing 物件 陣列,具有type
、key
和value
屬性。
重新導向會在檔案系統(包括頁面和 /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,
},
]
},
}
Header、Cookie 和 Query 比對
若要僅在 header、cookie 或 query 值也符合 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
header。
其他重新導向
- 在 API 路由 和 路由處理器 內,您可以根據傳入的請求重新導向。
- 在
getStaticProps
和getServerSideProps
內,您可以在請求時重新導向特定頁面。
版本歷史
版本 | 變更 |
---|---|
v13.3.0 | 新增 missing 。 |
v10.2.0 | 新增 has 。 |
v9.5.0 | 新增 redirects 。 |
這篇文章對您有幫助嗎?