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/<slug>), 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).

  • Thanks so much @Mats. I'm migrating an EE site and in the old site, when a user hit /help/ I would simply check for value in seg2, and if didn't exist, would specify a specific Entry_ID in the EE Entries tag. That would always display whatever Entry the client wanted at the /help/ index. So what I'm now doing in Craft (found in another question here) in these simple Sections is having help/index and help/_entry, where help/index just has a 301 redirect to /help/valid-index-slug-wanted-by-client A bit of a hack but accomplishes what the client wants. Oct 11 '16 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.