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.