Redirects in next.config.js
Unanswered
Belgian Hare posted this in #help-forum
Belgian HareOP
Hi guys, I was wondering how can I make a redirect with a nagavite lookahead in the slug.
Let's say I have these two routes:
-
-
What I want to do is, if you input in the URL
The problem is that if I include this in the
I tried to use this regex in the slug, but it didn't work...
Any idea of how can I achieve this or if there is a better way of doing it?
Also this is my file tree:
Let's say I have these two routes:
-
/blog/new-
/blog/:id/overviewWhat I want to do is, if you input in the URL
/blog/:id I want to redirect the user to /blog/:id/overview. The problem is that if I include this in the
next.config.js I cannot navigate to /blog/new because new is being inferred as the slug.const nextConfig = {
async redirects() {
return [
{
source: "/blog/:id",
destination: "/blog/:id/overview",
permanent: true,
}
];
},
};I tried to use this regex in the slug, but it didn't work...
const nextConfig = {
async redirects() {
return [
{
source: "/blog/:id(^(?!new$).*)",
destination: "/blog/:id/overview",
permanent: true,
}
];
},
};Any idea of how can I achieve this or if there is a better way of doing it?
Also this is my file tree:
blog/
├── [id]/
│ └── overview/
│ └── page.tsx
└── new/
└── page.tsx1 Reply
New Guinea Freshwater Crocodile
Try this:
"/blog/:id((?!new))". I think you don't need to wrap it with ^ or $, because next already does it for you