#Automatically redirect to /404 when visiting a page that doesn't exist

4 messages · Page 1 of 1 (latest)

strong hazel
#

Hey! I'd like to do a redirection to my /404 page each time a user visits a non existing page. I know I can create a 404.tsx file and render that component when this happens, but I have my not found page created and handled on builder.io, so I'd like to do a redirection there instead. What would be the best way of doing it?

gentle nova
#

I would look into a middleware in the top layout.tsx to see when nothing matched and then do redirect.

#

(but just guessing here)

strong hazel
#

Tried it with this code, but the problem is that even if I have the info about the pathname before loading the page, I don't have a way to check if that route exists until it fails, and after that I cannot do any action like a redirection to my /404 🤔