I am using EE2.9.2 with Stash and CE Cache. While trying to create a clean URL structure, ie. raising the appropriate 404s where necessary, I ran into challenges I'm struggling to solve.

I have a Books section (among other resources) where I've set up categories (e.g. Arts, Children, etc.). To keep this section 404-sane I need to check if the URL has the proper "books" in it and if the category exists.

After a lot of try and error, here's what I ended up doing:

1) Test for proper section
This is the template /resources/index.html
"/resources/books" should pass, "/resources/bookz" should not.

{if segment_2 != ""}
    {if "movies|books|music" *= segment_2}
        {!-- all is well (there is no "does not contain" directive in EE) --}

                {if no_results } {redirect="404"} {/if}

I'd love to use the commented line instead of the workaround, but if I enable CE Cache and enter a wrong URL nothing happens and the request eventually times out.

2) Test for proper category
This is the template I embed if {segment_3} is not empty.
"/resources/books/arts" should pass, but "/resources/books/artz" should not.

{if {exp:stash:is_empty name="current_category_name"}}
        $this->EE->output->set_header("HTTP/1.0 404 Not Found");

Again I'd love to use the commented line and not use PHP, but if I don't CE Cache caches the wrong URL.

I'm frustrated that something so elementary and necessary seems to hard.

Any advice on how to do this better or why the above doesn't work with the commented line instead of the workarounds is very much appreciated.

1 Answer 1


I would suggest that you stop trying to handle the routing logic in the template. It makes the templates more complicated than they need to be.

I highly recommend Resource Router for routing.

One nice approach for 404s is to define rules in Resource Router for all possible routes, and add this rule to the end to capture anything that does not match them:

".*" => function($router) {
  • Oh Mark, when are you going to publish your brain dump on Github? Thank you for this tip. I've installed the extension and it works beautifully.
    – Crafty Cat
    Jan 30, 2016 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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