0
www.cooking/recipes/apple_pie

The first Expression Engine segment (recipes) represent the recipes channel, and the second segment represent an entry (here apple_pie)

If an entry doesn't exist or is misspelled, i use this code to redirect to the 404 page, and it works fine:

{exp:channel:entries channel="recipes" limit="1" require_entry="yes"}
          {exp:ifelse parse="inward"}
                {if no_results}
                        {redirect="error/404"}
                {/if}
           {/exp:ifelse}
{/exp:channel:entries}


But if I add a segment (dessert), always have redirect to the 404 page, even if the entry exist. Without the redirect code the entries pages like (www.cooking/recipes/dessert/apple_pie) works fine, but i lose the redirect function.

www.cooking/recipes/dessert/apple_pie


What am I doing wrong here?

5
  • Lol, well what are desserts? Is that a channel category? Using Solspace Tag? Is desserts a different channel?
    – jrothafer
    Aug 18, 2015 at 18:41
  • Hi Jrothafe!! Desserts are something delicious !! Yes it's a channel category related to "recipes channel", not a Solspace tag, not a channel.
    – Stéphane
    Aug 18, 2015 at 18:50
  • hummmm it's a category,
    – Stéphane
    Aug 18, 2015 at 18:53
  • So do you want both www.cooking/recipes/dessert/apple_pie and www.cooking/recipes/apple_pie to load your apple_pie entry?
    – jrothafer
    Aug 18, 2015 at 18:58
  • only one: www.cooking/recipes/dessert/apple_pied. Dessert exist as a category, in this case the segment dessert is linked with a condition inside the main recipes template: if {segment_2} == 'dessert' (template code). So if the segment 2 = dessert the main recipes template will display the specific code for the specific condition (if {segment_2} == 'dessert').
    – Stéphane
    Aug 18, 2015 at 19:11

2 Answers 2

1

https://ellislab.com/expressionengine/user-guide/urls/template_routes.html

Template routes! These things are your friend. On your template, you want to set a template route that looks like this:

/recipes/{section:category}/{section:alpha_dash}

Set that template to require all routes. Then bam:

{exp:channel:entries channel="{segment_1}" category="{segment_2}" url_title="{segment_3}" limit="1" require_entry="yes"}
      {exp:ifelse parse="inward"}
            {if no_results}
                    {redirect="error/404"}
            {/if}
       {/exp:ifelse}
{/exp:channel:entries}

This should do the trick, no?

5
  • Jrothafer, thanks for your fast help and reply !! I thought about routes, but until today I did not catch how it works (lack of documentation). I will analyze with more calm today your solution, and at the same time i will try to catch Templates route eating desserts ;)
    – Stéphane
    Aug 18, 2015 at 19:42
  • The Ellislab Templates routes documentation is not very clear, it sounds incomplete to understand how it works. I tried the template routes but it returns a blank page with a message: undefined.....
    – Stéphane
    Aug 19, 2015 at 2:10
  • I'll see if I can do some testing on a dev server today.. No promises though!
    – jrothafer
    Aug 19, 2015 at 12:06
  • On a side note, I found the EE router much more understandable after I'd read and understood the CodeIgniter Router (after all, its what EE is built on!), which is documented also. Check it out it may help you out.
    – Blatant
    Aug 19, 2015 at 15:28
  • Hi Blatant, now I understand better the the EE router. Using 2 segments , the channel entries with require_entry="yes" to redirect if the entry no exists works fine. But it doesn't work using 3 segments. If I use a variable in the second segment in a route template: /recipes/{cat:alpha_dash}/{section:alpha_dash}, a blank screen appears with this message: This web page is not available : ERR_CONTENT_DECODING_FAILED. So i will stay with only 2 segment about entries and redirect.With 3 segments, it sounds complicated.
    – Stéphane
    Aug 19, 2015 at 19:52
1

Based on my understanding of the issue...

Try this:

{exp:channel:entries channel="recipes" url_title="{if segment_2 != 'dessert'}{segment_2}{if:else}{segment_3}{/if}" limit="1" require_entry="yes" dynamic="no"}

If there's only dessert category then I think this should work, unless you have other categories, then you'll have to update the if statement.

0

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.