0

背景:我有一个博客,其中包含大量被 Google 索引的内容。例如,所有内容都被索引为www.site.com/post1。我正在将我的博客迁移到 NextJS,并且我已将博客文章的范围限定为www.site.com/blog/post1。我能够轻松地使用 301 重定向来维护这些博客文章的 SEO。但是,我遇到了一个问题,其中www.site.com/sitemap.xml 之类的链接也被重定向到www.site.com/blog/sitemap.xml。只有当模式与某些路径不匹配时,有没有办法重定向?这是我在 next.config.js 中关于重定向的部分

async redirects() {
    return [
      {
        source: '/:slug',
        destination: '/blog/:slug',
        permanent: true// Matched parameters can be used in the destination
      },
      {
        source: '/sitemap.xml',
        destination: '/sitemap.xml',
        permanent: false// Matched parameters can be used in the destination
      }
    ]
  }
4

1 回答 1

0

我认为顺序很重要,您是否尝试过将站点地图规则放在首位?

async redirects() {
    return [
      {
        source: '/sitemap.xml',
        destination: '/sitemap.xml',
        permanent: false// Matched parameters can be used in the destination
      },
      {
        source: '/:slug',
        destination: '/blog/:slug',
        permanent: true// Matched parameters can be used in the destination
      }
    ]
  }

否则,可能需要使用正则表达式

async redirects() {
    return [
      {
        source: `/:slug(^((?!sitemap\.xml).)*$)`,
        destination: '/blog/:slug',
        permanent: true// Matched parameters can be used in the destination
      },
      {
        source: '/sitemap.xml',
        destination: '/sitemap.xml',
        permanent: false// Matched parameters can be used in the destination
      }
    ]
  }
于 2021-09-01T19:18:21.560 回答