Redirects in next.config.js are ignored

Hi,

In my next.config.js file, I specified redirects for some of my pages. However, when deploying to Netlify, it seems to ignore these redirects. Should I use _redirects instead so Netlify can acknowledge them?

Here’s what my next.config.js file looks like:

const withBundleAnalyzer = require("@next/bundle-analyzer")({
  enabled: process.env.ANALYZE === "true",
});

module.exports = withBundleAnalyzer({
  devIndicators: {
    autoPrerender: true,
  },
  images: {
    domains: ["dl.airtable.com", "cdn.sanity.io"],
  },
  webpack: (config, options) => {
    config.optimization.minimize = true;
    config.optimization.providedExports = true;
    config.optimization.usedExports = true;
    config.optimization.sideEffects = true;
    return config;
  },
  target: "serverless",
  reactStrictMode: true,
  async redirects() {
    return [
      {
        source: "/course/",
        destination: "/courses",
        permanent: true,
      },
      {
        source: "/contact-us/",
        destination: "/support/contact-us",
        permanent: true,
      },
      {
        source: "/donate",
        destination: "https://google.org",
        permanent: true,
      },
      {
        source: "/app",
        destination: "https://apple.com",
        permanent: true,
      },
      {
        source: "/legal/tos",
        destination: "/legal/terms-of-service",
        permanent: true,
      },
    ];
  },
});

Any help would be appreciated, thanks!

Hi @p0w3r_zurg3 :wave:

Like you’ve alluded to, I believe that redirects on Netlify have to be defined using a _redirects file.

I don’t think there’s a way to get it to respect the redirects you’ve placed in the next.config.js file.

The Netlify Next.js doc also seems to support this, since it links to that page under the “Redirects & rewrites” heading.

Hope that gets you pointed in the right direction!

1 Like

@birdcar thank you so much!

1 Like