Redirecting "if entry is not defined" returns 404 instead All the documentation, and answers here say this should work... But it returns the 404 instead of redirecting and I can't figure out why?


Section: Structure = help 
Entry URL Format: Top-Level Entries =
help/{slug} 
Entry URL Format: Nested Entries = help/{slug} 
Entry 'sTemplate = help/_entry


Top line of the "help/_entry.html" template contains

{% if entry is not defined %}
    {% redirect "/help/welcome-to-help" 301 %}
{% endif %}


where "welcome-to-help" is an Entry slug in the Help Section.

If I hit "example.com/help/welcome-to-help/" (or any other valid Slug), everything works fine. But if I hit "example.com/help/asdf/" it returns the 404 instead of redirecting. 

Am at a loss at this point??
The reason you're getting a 404 is because Craft isn't even hitting your help/_entry.html template. Because there's no entry matching the URI /help/asdf, the template will never render and the request will go straight to 404.

The solution is to create a dynamic route matching your section's URL format (i.e. help/&lt;slug&gt;), and point it to the same template.

This works, because whenever Craft can't match a URI to an entry or category, it'll then try to render any dynamic route matching the URI's format (for a deeper understanding of how Craft manages routes and requests, I'd recommend looking at the the official docs' page on routing).