trailingSlash

預設情況下,Next.js 會將帶有結尾斜線的 URL 重新導向至不帶斜線的對應網址。例如 /about/ 會重新導向至 /about。您可以配置此行為使其反向運作,讓不帶結尾斜線的 URL 重新導向至帶斜線的對應網址。

開啟 next.config.js 並新增 trailingSlash 設定:

next.config.js
module.exports = {
  trailingSlash: true,
}

設定此選項後,像 /about 這樣的 URL 將會重新導向至 /about/

當使用 trailingSlash: true 時,某些 URL 屬於例外情況且不會附加結尾斜線:

  • 靜態檔案 URL,例如帶有副檔名的檔案。
  • 位於 .well-known/ 下的任何路徑。

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

當與 output: "export" 配置一起使用時,/about 頁面將輸出為 /about/index.html(而非預設的 /about.html)。

版本歷史

版本變更
v9.5.0新增 trailingSlash

On this page