Next.js redirect all paths except one

Viewed 1060

I want to create a redirect for all subroutes /a, /b, /c but not /api.

So my basic setup looks like this:

{
  source: '/:path*',
  destination: 'https://otherdomain.com/:path*',
  permanent: false
}

What do I have to change to stop redirecting /api/* from here?

I tried some hacks like :path(?!api$)* but none of them are working.

1 Answers

You can try with regex.

In documentaion is a small example nextjs docs

In this github issue is a example as well.

This regex match everything expect api.

^(?!.*\bapi\b).*$

In source probably its loks like that.

 source: '/:host(^(?!.*\bapi\b).*$)/:path*',
 destination: 'https://otherdomain.com/:host/:path*',
Related