0

Context: I have a blog with a good amount of content indexed by Google. For example, all the content is indexed as www.site.com/post1. I am migrating my blog to NextJS and I have scoped the blog posts to www.site.com/blog/post1. I was able to easily use the redirects with 301 to maintain the SEO for these blog posts. However, I am running into an issue where links like www.site.com/sitemap.xml are also redirected to www.site.com/blog/sitemap.xml. Is there a way to redirect only if the pattern doesn't match some path? Here is my section on redirects in 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
      }
    ]
  }
Shrikar
  • 840
  • 1
  • 8
  • 30

1 Answers1

0

I would think order would matter, have you tried putting your sitemap rule first?

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
      }
    ]
  }

Otherwise, may need to use regex

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
      }
    ]
  }
John
  • 1,240
  • 9
  • 13