You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js路由重写问题:非根路径无法自动指向main文件夹

解决Next.js路由重写问题

你的问题出在重写规则没有传递路径参数,导致除根路径外的其他请求无法正确映射到main文件夹下的对应页面。修正后的next.config.mjs配置如下:

/** @type {import('next').NextConfig} */
const nextConfig = {
  async rewrites() {
    return [
      {
        source: '/dashboard/:path*',
        destination: '/dashboard/:path*',
      },
      {
        source: '/:path*',
        destination: '/main/:path*',
      },
    ];
  },
};

export default nextConfig;

关键修正点:

  • 对于/dashboard/:path*规则,将destination改为/dashboard/:path*,确保访问/dashboard/xxx时能正确映射到dashboard文件夹下的xxx页面。
  • 对于通用规则/:path*,将destination改为/main/:path*,这样用户访问/faq时,会自动映射到main/faq页面,URL中不会显示main前缀。

原来的配置中,destination没有携带:path*参数,导致所有非dashboard的请求都被重定向到/main根页面,而非对应的子页面,这就是访问/faq无法正常加载的原因。

内容的提问来源于stack exchange,提问作者mkHun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:07:37