redirects
重定向允许你将传入请求路径重定向到不同的目标路径。
要使用重定向,你可以在 next.config.js
中使用 redirects
键:
next.config.js
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