Search code examples
http-redirectnuxt.jsnetlify

Netlify redirects not working with Nuxt 3 static site


I have generated and deployed a static site using Nuxt 3 on Netlify. To, keep some old routes active, I have added some redirect rules in netlify.toml file, like this:

[[redirects]]
  from = "/tags/"
  to = "/posts/topics/"
  status = 301

and the site generation command was nuxt generate with ssr: true in nuxt.config.ts file. The redirect rules are successfully discovered by Netlify as shown in their Production Deploy log.

The redirects are not working in production. For example, site.com/tags/ should redirect to site.com/posts/topics/ but I guess Nuxt is catching the route and showing 404 page instead of redirecting.


Solution

  • I've found and solved the issue and I think it should be shared as an answer.

    In the Netlify configuration file netlify.toml, there was a redirect rule for 404 status code before other redirect rules like this:

    [[redirects]]
     from = "/*"
     status = 404
     to = "/404.html"
    
    [[redirects]]
     from = "/other"
     to = "/new/other"
    

    Removing the rule for 404 and placing it at the end of all redirect rules solved the problem. Apparently, the issue was not with Nuxt.js but about the ordering of redirect rules in Netlify.

    New redirect rule:

    [[redirects]]
     from = "/other"
     to = "/new/other"
    
    [[redirects]]
     from = "/*"
     status = 404
     to = "/404.html