Redirects - how to 301 redirect wildcards (aka *) instead of throwing 404

Say I have a blog at /blog. I would like any non-existent links, like say blog/doesnt-exist to redirect back to /blog rather than go to my 404 page.

I thought I could accomplish this by setting /blog/* /blog 301 but I still get a 404 if I try a non-existent link. I’ve tried 301!, 404, 200 and not achieving the desired results.

I know that if I explicitly write out rules for each route, like /blog/no-exist /blog 301 then it will work fine. But that would be cumbersome and not a “catch-all” solution.

How would I go about setting up a bucket to redirect ALL incorrect/non-existent links at a particular route to another route, rather than a 404.

For context, this is a Gatsby project that is already deployed to Netlify, where I have a _redirects file with other working redirect rules.

Since Gatsby is handling the /blog route already and you are getting the 404 path through the Gatsby 404.js page, maybe you can handle this without a redirect by changing the route behavior of the Gatsby app.

Maybe ask the Gatsby community how to handle that case.

I’m not sure there is a redirect for a no-exist condition of a static route on Netlify other than just forcing the redirect if it is not a valid path. That would kinda go against the way the Gatsby SPA works, since it wants the rewrite back to the /index.html for client side routing.

1 Like