默认情况下,Next.js 会将带有尾部斜杠的 URL 重定向到不带尾部斜杠的对应 URL。例如,/about/ 将重定向到 /about。您可以将此行为配置为相反的方式,即不带尾部斜杠的 URL 将重定向到带有尾部斜杠的对应 URL。
打开 next.config.js 并添加 trailingSlash 配置:
module.exports = {
trailingSlash: true,
}设置此选项后,/about 等 URL 将重定向到 /about/。
当使用 trailingSlash: true 时,某些 URL 是例外情况,不会在其末尾附加尾部斜杠:
.well-known/ 下的任何路径。例如,以下 URL 将保持不变:/file.txt、images/photos/picture.png 和 .well-known/subfolder/config.json。
当与 output: "export" 配置一起使用时,/about 页面将输出 /about/index.html(而不是默认的 /about.html)。
| 版本 | 变更 |
|---|---|
v9.5.0 | trailingSlash 已添加。 |