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

Next.js重定向多余添加locale前缀及/pl/*全局重定向如何配置

问题根因

Next.js 的i18n路由默认会给所有重定向规则自动拼接当前 locale 前缀,这就是你访问 /restauracja 被重定向到 /pl/restaurant 的原因。

解决方案

两步修改next.config.js配置即可:

  1. 给现有重定向规则新增locale: false参数,禁用自动拼接locale前缀的逻辑
  2. 新增全局重定向规则,将所有带/pl/前缀的路径301重定向到无默认locale前缀的路径

完整配置示例

/** @type {import('next').NextConfig} */
const nextConfig = {
  i18n: {
    locales: ["pl", "en", "de"],
    localeDetection: false,
    defaultLocale: "pl",
  },
  async redirects() {
    return [
      // 全局默认locale前缀重定向,放在规则最前面保证优先级
      {
        source: "/pl/:path*",
        destination: "/:path*",
        permanent: true,
        locale: false, // 必须添加,避免重定向死循环
      },
      // 原有业务重定向规则,每个都加locale: false
      {
        source: "/restauracja",
        destination: "/restaurant",
        permanent: true,
        locale: false,
      },
      {
        source: "/klub-pub",
        destination: "/club",
        permanent: true,
        locale: false,
      },
      {
        source: "/kawiarnia",
        destination: "/cafe",
        permanent: true,
        locale: false,
      }
    ];
  }
}

module.exports = nextConfig

生效后验证

  • 访问 /restauracja 会直接301重定向到 /restaurant,无多余/pl前缀
  • 访问 /pl/xxx 任意路径都会301重定向到 /xxx,彻底解决默认locale重复路径的SEO问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 13:36:03