I found this helpful link that cover many solution for this issue:
https://www.youtube.com/watch?v=_vSMITiXAik
Solution 1:
- use rewirte function in your
next.config.js
return [
{
source: "(c|C)(o|O)(n|N)(t|T)(a|A)(c|C)(t|T)",
destination: "/Contact", // here you need to add the exact page contact or Contact
},
];
},
- use the
_middleware
feature:
inside you pages folder add a _middleware.ts
file.
import { NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === request.nextUrl.pathname.toLocaleLowerCase())
return NextResponse.next();
return NextResponse.redirect(
`${request.nextUrl.origin}${request.nextUrl.pathname.toLocaleLowerCase()}`
);
}
with this solution you can need to rename your page to be lowercase.
- use middleware feature with a folder for each page.
you need to keep the _middleware.ts
the same and you need to do those steps:
- create a folder
contact
all lowercase.
- move your page inside this folder.
- add an
index.ts
that point to your page.
its conent should be something like this:
export { default } from "./contact";
- rename all your page with this extension:
.page.tsx
or page.ts
if you use typescript and .page.jsx
or page.js
if javascript.
- in
next.config.js
you need to add this pageExtensions: ["page.tsx", "page.ts"]
if you use typescript and pageExtensions: ["page.jsx", "page.js"]
if you use javascript.
you need to do this for all your pages.
/Contact
would give a 404 while/contact
works. It's redirects that are case-insensitive. (See github.com/vercel/next.js/pull/8848, github.com/vercel/next.js/discussions/15539, reddit.com/r/nextjs/comments/hk2qmk/…). – Welleszredirects
(not routing) are not case-sensitive. – Jordans