Next.js Discord

Discord Forum

Redirecting to basePath

Answered
Checkered Giant posted this in #help-forum
Open in Discord
Checkered GiantOP
Hi there,
I want to use basePath in my Next.js application: /memories.
But I'm afraid that the link to my page that my clients have will break. So I would like to redirect all calls to my page, that doesn't contain /memories prefix in the url to include /memories prefix.

I was thinking to use redirects() function inside next.config.ts file, but I'm having troubles creating a correct regex.
Here is something that I worked with something like this
async redirects() {
    return [
      {
        source: '/\\(\\?!memories\\(/|$\\)\\).\\*$',
        destination: '/temp',
        permanent: true,
        basePath: false,
      },
    ];
  },
but it doesn't works (like the path is not matched)

Could you help me debug this regex or propose easier solution?
Answered by Tanzim Hossain
js 
async redirects() {
  return [
    {
      source: '/',
      destination: '/memories',
      permanent: true,
      basePath: false,
    },
    {
      source: '/:path((?!memories).*)',
      destination: '/memories/:path*',
      permanent: true,
      basePath: false,
    },
  ];
}

try this approch
View full answer

3 Replies

js 
async redirects() {
  return [
    {
      source: '/',
      destination: '/memories',
      permanent: true,
      basePath: false,
    },
    {
      source: '/:path((?!memories).*)',
      destination: '/memories/:path*',
      permanent: true,
      basePath: false,
    },
  ];
}

try this approch
Answer
Checkered GiantOP
Works great! Thank you!!