如何使 NextJS 中的页面路径不区分大小写

Moh*_*ith 5 javascript reactjs next.js

pages我在名为 的文件夹下有一个文件about.tsx。所以该页面的路径是/about,我可以通过访问 来访问该页面example.com/about。但是,如果我访问example.com/About,它会重定向到 404 页面。

我检查了 Nextjs 存储库,似乎这是预期的行为。因此,是否有一种解决方法可以使路径不区分大小写,以便example.com/About也可以正常工作并将用户定向到该/about页面?

ngh*_*aht -1

我同意这是 Next.js 的行为,它们只处理确切的页面名称,about而不是两者aboutAbout使用相同的文件page/about.tsx

但解决方案是您按照本指南继续实现主页(例如:about.tsx)并设置其他页面以重定向到该页面(例如:关于 -> 关于)https://nextjs.org/docs/api-reference /next.config.js/重定向

// next.config.js
module.exports = {
  async redirects() {
    return [
      {
        source: '/About',
        destination: '/about',
        permanent: true,
      },
    ]
  },
}

// Set permanent:true for 301 redirect  & clean SEO!
Run Code Online (Sandbox Code Playgroud)

  • 这将导致无限重定向循环 (3认同)