404 and redirects with netlify?

I'm struggling with my redirects instead resolving as 404s I want to 404 except for known redirects. I have a static site, any ideas?
5 Replies
kapa.ai
kapa.ai4w ago
Hi @exopenguin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources, there's a specific solution for handling redirects and 404s with Netlify and Nuxt static sites. The issue you're experiencing is likely related to the ordering of redirect rules in your netlify.toml file. According to Netlify redirects not working with Nuxt 3 static site, if you have a catch-all 404 redirect rule placed before your other redirect rules, it will intercept all requests and prevent your intended redirects from working. Solution Make sure your redirect rules in netlify.toml are ordered correctly - place your specific redirects before the catch-all 404 rule:
# Specific redirects first
[[redirects]]
from = "/old-path"
to = "/new-path"
status = 301

# 404 catch-all at the end
[[redirects]]
from = "/*"
status = 404
to = "/404.html"
# Specific redirects first
[[redirects]]
from = "/old-path"
to = "/new-path"
status = 301

# 404 catch-all at the end
[[redirects]]
from = "/*"
status = 404
to = "/404.html"
The catch-all 404 rule should always be at the end of all your redirect rules. If it's placed at the beginning, it will match all routes before your specific redirects have a chance to be evaluated. This is a Netlify configuration issue rather than a Nuxt issue - the redirect rules are processed by Netlify before Nuxt handles the routing. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@393928198943145984> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
exopenguin
exopenguinOP4w ago
surely that would render all pages with a 404? @kapa.ai surely that would render all pages with a 404?
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're right to question that! Let me clarify - the catch-all 404 rule with from = "/*" and status = 404 only applies when no other route matches. It doesn't render all pages with a 404. According to Netlify redirects not working with Nuxt 3 static site, Netlify processes redirect rules in order from top to bottom. When a request comes in: 1. Netlify checks each redirect rule sequentially 2. If a specific redirect matches (like /old-path/new-path), it executes that redirect 3. If your Nuxt static site has a matching page/route, that page is served normally 4. Only if nothing else matches does the catch-all /* rule trigger, sending the request to your 404 page The key is that the catch-all 404 rule acts as a fallback - it only catches requests that don't match any of your: - Specific redirect rules (defined above it) - Actual pages in your static site So your existing pages will continue to work normally, your defined redirects will work, and only truly non-existent routes will hit the 404 page. The ordering ensures your specific redirects are evaluated before the catch-all fallback. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@393928198943145984> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?