【问题标题】:Redirect without locale in URL nextjs在 URL nextjs 中不使用语言环境进行重定向
【发布时间】:2022-01-03 23:08:10
【问题描述】:

当我以这种方式重定向时:

{
        source: "/restauracja",
        destination: "/restaurant",
        permanent: true,
      },

它将我重定向到 /pl/restaurant

如何在没有第二个“pl”的情况下修复它并重定向?它仅在重定向时发生,通常默认本地不会在 url 中显示自己。这对 SEO 来说是个问题,因为它会复制网站。我想做从 domain.pl/pl/* 到 domain.pl/* 的 301 重定向的全局设置,但不知道该怎么做,我无法通过搜索找到解决方案。

在 next.config.js 中的样子:

...
i18n: {
    // These are all the locales you want to support in
    // your application
    locales: ["pl", "en", "de"],
    localeDetection: false,
    // This is the default locale you want to be used when visiting
    // a non-locale prefixed path e.g. `/hello`
    defaultLocale: "pl",
    // This is a list of locale domains and the default locale they
    // should handle (these are only required when setting up domain routing)
  },
  async redirects() {
    return [
      {
        source: "/restauracja",
        destination: "/restaurant",
        permanent: true,
      },
      {
        source: "/klub-pub",
        destination: "/club",
        permanent: true,
      },
      {
        source: "/kawiarnia",
        destination: "/cafe",
        permanent: true,
      }
    ];
  },
});

感谢您的帮助

【问题讨论】:

    标签: javascript reactjs redirect next.js i18next


    【解决方案1】:

    在我看来,本地化路径(如 restauracja 作为 restaurant 的翻译页面)对于 NextJs built-in i18n system 来说是不可能

    这里列出了哪些是可能的,哪些是不可能的(显然):

    --- path --------- language -- possible -- remark ------------
    /pl/restauracja  |    PL    |    no    | (translated path with locale, )
    /restauracja     |    PL    |    no    | (translated path without locale)
    /pl/restaurant   |    PL    |    yes   | (default path with locale)
    /restaurant      |    EN    |    yes   | (default path without locale)
    /en/restaurant   |    EN    |    yes   | (default path with locale)
    

    我找不到任何可靠的信息(无论是如何做,也没有“不支持本地化路径”之类的评论),但我发现的所有内容看起来都不受支持。

    我完全禁用了内置的 i18n 系统,并自己实现了整个事情。

    【讨论】:

    • 您好,谢谢您的回答。问题是,在我们使用链接“restauracja”之前,但现在我想将其重定向到“restaurant”,因为我们不再使用“restauracja”。但不管我重定向哪个链接,哪个不再存在,它总是以双“pl”开头,我希望它们像其他默认页面一样开始 - 没有第二个“pl”。
    • 看看我们的页面:gopos.pl - 所有子页面都像gopos.pl/restaurantgopos.pl/gohub等等。如果我换成另一种语言,比如 EN - gopos.pl/en/restaurant。但是为什么当我从'restaurant'重定向时,它会使用双'pl'重定向-gopos.pl/pl/restaurant如果'pl'是一种默认语言。我试图为它找到解决方案,修复它,因为现在它对 SEO 有不好的影响。也许您可以在我重定向时设置从 gopos.pl/pl/* 到 gopos.pl/* 的全局 301 重定向?
    • 哦,好吧,我完全误解了你的意思。你可以试试locale: false 吗?请注意,添加 sitemap.xml 和/或“规范链接”可能是更直接的 SEO 方法。但这是一个全新的复杂故事。
    • 我认为额外的 '/pl/' 是一种“正确”的本地化 url,没有 '/pl/' 是别名。请注意,您的所有其他默认页面也可以与“/pl/”一起使用。
    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 2021-09-27
    • 2013-09-19
    • 2013-12-01
    • 1970-01-01
    • 2021-12-26
    • 2022-10-03
    • 2018-09-10
    相关资源
    最近更新 更多