K. D.
K. D.

Reputation: 4249

Next.js redirect all paths except one

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.

Upvotes: 5

Views: 5447

Answers (1)

Robert
Robert

Reputation: 2763

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*',

Upvotes: 5

Related Questions