0

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:else}
        {!--redirect="404"--}

        {!-- TEMPORARY WORKAROUND--}
        {exp:channel:entries
            channel="resources"
            url_title="does_not_exist"
            disable="categories|category_fields|custom_fields|member_data|pagination"}
                {if no_results } {redirect="404"} {/if}
        {/exp:channel:entries}
    {/if}
{/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"}}
    <?php
        $this->EE->output->set_header("HTTP/1.0 404 Not Found");
    ?>
    {!--redirect="404"--}
    {redirect="error/error-404"}
{/if}

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

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) {
   $router->set404();
}
1
  • 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 that you have read and understand our privacy policy and code of conduct.

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