Next.js 重定向除一个之外的所有路径

K. *_* D. 5 next.js

我想为所有子路由 /a、/b、/c 创建重定向,但不为/api 创建重定向。

所以我的基本设置如下所示:

{
  source: '/:path*',
  destination: 'https://otherdomain.com/:path*',
  permanent: false
}
Run Code Online (Sandbox Code Playgroud)

我需要更改什么才能停止/api/*从这里重定向?

我尝试了一些类似的技巧:path(?!api$)*,但没有一个起作用。

Rob*_*ert 5

您可以尝试使用正则表达式。

文档中有一个小示例nextjs 文档

在这个github 问题中也有一个例子。

这个正则表达式匹配 api 之外的所有内容。

^(?!.*\bapi\b).*$
Run Code Online (Sandbox Code Playgroud)

在源代码中可能是这样的。

 source: '/:host(^(?!.*\bapi\b).*$)/:path*',
 destination: 'https://otherdomain.com/:host/:path*',
Run Code Online (Sandbox Code Playgroud)