0

I'm having problems with a template sending a missing page to another segment rather than the 404 page. For example, The URL below would give us our Disney Ship.

WEBSITE/cruises/disney/ships/disneyship/

What happens if someone types this?

WEBSITE/cruises/disney/ships/disneyshipTHISSHOULDNOTBEHERE/

It should 404 correct? Instead, it keeps the URL above in the browser but displays the contents of segment 2.

I've been testing all over the place and slimmed down the code for testing to just this:

{exp:channel:entries channel="at_cruise_ships" require_entry="yes"}
    {if no_results}
        {redirect="404"}
    {/if}
  <h2>{title}</h2>
    {body}
{/exp:channel:entries}

NOTES:

  • this works great if i do the same scenario in segment 2. For example, http://www.example.com/cruises/disneyTEST/ will redirect to the 404 page as expected
  • the disney ship exists in the cruise_ships channel
  • strict URLs is enabled in preferences
  • my 404 template is assigned in preferences
  • I am using Structure, but not directly related to this page

Please let me know if you have any suggestions on this as i'm at a loss. Thanks!

2 Answers 2

1

The problem you're facing is related to the fact that the Channel Module doesn't look for entries on all segments by default.

Imagine 9 segments. The module would have to test every single segment until find one entry matching it. This could make the system really slow. So, by default, it just looks for entries matching the most common used segments for it: 2 and 3.

But you can still use other segments. You just need to explicitly choose one. You need to set the parameters dynamic as no and url_title as the segment intended.

If you want to use the segment_4 as your url_title, you need to make it explicit:

{exp:channel:entries channel="at_cruise_ships" require_entry="yes" dynamic="no" url_title="{segment_4}"}
    {if no_results}
        {redirect="404"}
    {/if}
  <h2>{title}</h2>
    {body}
{/exp:channel:entries}

The require_entry parameter will trigger the {if no_results} conditional if no entry was found and only in this case.

No matter how you set your URL structure, you need to deal with extra segments by yourself:

{!-- for all extra segments beyond 4th --}
{if segment_5}
 {redirect="404"}
{/if}
3
  • I don't understand this? Why would EE not use the same logic on all segments? How would you redirect a mistyped page in segment 4 then if the conditional will not run? Commented Jun 1, 2015 at 14:42
  • BTW.. the code above didn't work... it's still doing the same thing. Commented Jun 1, 2015 at 14:42
  • The conditional run if no entry was found. It doesn't matter in which segment. Which segment points to the entry will need? The 4th?
    – Sobral
    Commented Jun 1, 2015 at 14:55
-1

You can define the 404 page in the control panel so you don't have to do the redirect. EE will do it for you. https://ellislab.com/expressionengine/user-guide/urls/404pages.html

2
  • I already did all of this. I have to use this code for it to redirect: Commented May 29, 2015 at 19:06
  • {if no_results} {redirect="404"} {/if} Commented May 29, 2015 at 19:06

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.