Skip to content

trailingSlash

默认情况下,Next.js 会将带有尾部斜杠的 URL 重定向到没有尾部斜杠的 URL。例如 /about/ 将重定向到 /about。你可以将此行为配置为相反的方式,其中没有尾部斜杠的 URL 被重定向到带有尾部斜杠的 URL。

¥By default Next.js will redirect URLs with trailing slashes to their counterpart without a trailing slash. For example /about/ will redirect to /about. You can configure this behavior to act the opposite way, where URLs without trailing slashes are redirected to their counterparts with trailing slashes.

打开 next.config.js 并添加 trailingSlash 配置:

¥Open next.config.js and add the trailingSlash config:

js
module.exports = {
  trailingSlash: true,
}

设置此选项后,像 /about 这样的 URL 将重定向到 /about/

¥With this option set, URLs like /about will redirect to /about/.

当使用 trailingSlash: true 时,某些 URL 是例外,不会附加尾随斜杠:

¥When using trailingSlash: true, certain URLs are exceptions and will not have a trailing slash appended:

  • 静态文件 URL,例如带扩展名的文件。

    ¥Static file URLs, such as files with extensions.

  • .well-known/ 下的任何路径。

    ¥Any paths under .well-known/.

例如,以下 URL 将保持不变:/file.txtimages/photos/picture.png.well-known/subfolder/config.json

¥For example, the following URLs will remain unchanged: /file.txt, images/photos/picture.png, and .well-known/subfolder/config.json.

当与 output: "export" 配置一起使用时,/about 页面将输出 /about/index.html(而不是默认的 /about.html)。

¥When used with output: "export" configuration, the /about page will output /about/index.html (instead of the default /about.html).

版本历史

¥Version History

版本更改
v9.5.0trailingSlash 已添加。

Next.js v15.2 中文网 - 粤ICP备13048890号