Next.js Discord

Discord Forum

Redirects in next.config.js

Unanswered
Belgian Hare posted this in #help-forum
Open in Discord
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:
- /blog/new
- /blog/:id/overview

What 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.tsx

1 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