5

I'm interested in redirecting to a 404 if no category exists at the URL, for example if category xyz doesn't exist at; mydomain.tld/shop/category/xyz

I'm using Switchee and a case value for category as follows;

{exp:switchee variable="{segment_2}" parse="inward"}
    {case value="category"}
        {!-- Redirect to 404 if no category exists here --}
    {/case}
{/exp:switchee}

Wondering if anyone knows how to do a redirect here if a matching category is not found?

Thanks.

2 Answers 2

7

Get Low Seg2Cat, make sure the Set All Segments setting is set to Yes, and then use something like this:

{if segment_2 == 'category' AND segment_3_category_id == ''}
  {redirect="404"}
{/if}

...or the Switchee equivalent.

1
  • Awesome, thanks Low. Already had Seg2Cat installed so that was simples. :D Dec 5, 2012 at 11:02
2

I didn't have time to write this out before you chose Low's answer though, I will add it just the same for future reference:

{exp:switchee variable="{segment_2}/{segment_3}" parse="inward"}
    {case value="#^category/\S+$#"}
        your code....
    {/case}
    {case default="yes"}
        {redirect="404"}
    {/case}
{/exp:switchee}

This assumes you will always have something in segment 3 or else go 404

1
  • 1
    Low's version checks that a category actually exists though, rather than just that there is a string in segment 3 and that segment 2 is a category... think the OP needs a hybrid of both answers.
    – Tom Davies
    Dec 5, 2012 at 11:38

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.