How to return a 404 page for a given route?
-
I'm using Categories as my home page route so have no need for the example.com/categories page. I thought hey, why not have Nginx return a 404 page when users try to visit
/categories
:location /categories { return 404; }
But my bright idea isn't quite working because I get the standard ugly Nginx 404 template:
Does anyone know how I can get the Nodebb 404 template to show instead? For example:
-
There should be A way to make that appear by the nginx config, googling "nginx custom 404 error" might help
-
@Kowlin thanks but I don't really want a custom Nginx 404 page. I'd prefer the NodeBB 404 template (2nd screenshot) to be used.
-
@henrywright I'll see if I can somewhat fake it with bootstrap & css
-
You may try redirecting to the
/404
route. It might not look like exactly what you want:location /categories { return 301 $scheme://your.site/404; }
It will give you something like this
If you want it to say /categories not found, you could try redirecting to
/categoríes
(replacingi
withí
, i-acute)
-
@pitaj neat idea! I'll try that. But I'm also thinking there must be a way of intercepting the request before NodeBB serves the page. Then forcing a NodeBB style 404 (i.e. no redirecting)?
-
@henrywright I think you'd have to do so within NodeBB itself. Also, I don't suggest disabling any routes NodeBB may use, as they are hardcoded and 404ing them could mess stuff up.
-
You can make a plugin and add a handler for /categories and render 404 page.
-
@baris I'm going to give that a shot!
-
@pitaj I did some testing today with /categories 404'd. Seems as though things continued to work fine. I'm not disabling the functionality, just access to the page.