简体   繁体   English

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

[英]How to make path for pages in NextJS case insensitive

I have a file under the pages folder named about.tsx .我在pages文件夹下有一个名为about.tsx文件。 So the path for the page is /about and I'm able to access the page by visiting example.com/about .所以页面的路径是/about ,我可以通过访问example.com/about来访问该页面。 However, if I visit example.com/About , it will redirect to a 404 page.但是,如果我访问example.com/About ,它将重定向到 404 页面。

I've checked the Nextjs repo, seems like this is the expected behavior.我检查了 Nextjs 存储库,似乎这是预期的行为。 Therefore, is there a workaround that can make the path case insensitive so that example.com/About will also work and direct users to the /about page?因此,是否有一种解决方法可以使路径不区分大小写,以便example.com/About也可以工作并将用户定向到/about页面?

I agree that's Next.js's behavior, they only handle exact page name about instead of both about & About using the same file page/about.tsx我同意这是 Next.js 的行为,他们只处理确切的页面名称about而不是about & About使用相同的文件page/about.tsx

But the solution is you keep implementing a main page (eg: about.tsx) and setup other pages to redirect to that page (eg: About -> about) following this guide https://nextjs.org/docs/api-reference/next.config.js/redirects但解决方案是按照本指南https://nextjs.org/docs/api-reference继续实施主页(例如:about.tsx)并设置其他页面以重定向到该页面(例如:关于 -> 关于) /next.config.js/redirects

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

// Set permanent:true for 301 redirect  & clean SEO!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM