I have a page on my site: mysite.com/index.php/staff/staff-bio/john-smith

I'm using this 404 redirect:

{if no_results OR segment_2 != "staff-bio" OR segment_4 != ""}{redirect="404"}{/if}

On a related note, I'm using the require_entry="yes" option in my staff-bio template.

This redirect statement catches the problem it staff-bio is not segment 2, and if extra segments are added to the end, but if I were to change "john-smith" to just "john" in the url, or enter some other invalid path, it doesn't redirect. It just goes to a blank template page.

What am I doing wrong here?

up vote 4 down vote accepted

This looks like a bug on the no_results conditional, to me.

May I suggest you something? Let's change your approach to this. Look at the docs. You should take advantage of the parsing order and do not let your code run if you can avoid. So, use simple conditionals around the channel:entries variable pair.

This way, in the two first situations, the parsing process will pass right from the stage 13, simple conditionals, to the 20, redirect variable:

{if segment_4}{redirect="404"}{/if}
{if segment_2 != "staff-bio"}{redirect="404"}{/if}
{if segment_2 == "staff-bio"}
    {if segment_4 == ""}
        {exp:channel:entries [...] require_entry="yes"}
            {if no_results}{redirect="404"}{/if}
        {/exp:cahnnel:entries}
    {/if}
{/if}
  • man, you guys are pro! thank you so much! – hyphen81 Jun 13 '13 at 10:31
  • ok, so now I'm getting error messages. If I delete the last set of conditionals it doesn't give errors, but then its back to not redirecting again. – hyphen81 Jun 13 '13 at 11:39
  • here's the first error: A PHP Error was encountered Severity: Warning Message: Creating default object from empty value Filename: libraries/Template.php Line Number: 2508 – hyphen81 Jun 13 '13 at 11:40
  • and 2 more errors as well. A PHP Error was encountered Severity: Warning Message: Cannot modify header information - headers already sent by (output started at ...system/codeigniter/system/core/Exceptions.php:170) Filename: core/Common.php Line Number: 408 A PHP Error was encountered Severity: Warning Message: Cannot modify header information - headers already sent by (output started at ...system/codeigniter/system/core/Exceptions.php:170) Filename: libraries/Functions.php(680) : eval()'d code Line Number: 2 – hyphen81 Jun 13 '13 at 11:42
  • there appears to be a bug about this particular issue...support.ellislab.com/bugs/detail/18304 – hyphen81 Jun 13 '13 at 12:11

Robson's suggestion is on the money. Another approach would be to use the awesome IfElse add-on so parsing is less of an issue.

Using your example, it would look something like this:

{exp:ifelse parse="inward"}    
    {if segment_4 != "" || segment_2 != "staff-bio"}
        {redirect="404"}
    {if:elseif segment_2 == "staff_bio"}
        {if segment_4 == ""}
            {exp:channel:entries [...] require_entry="yes"}
                {no_results}{redirect="404"}{/no_results}
            {/exp:cahnnel:entries}
        {/if}
    {/if}
{/exp:ifelse}

I'm not 100% sure you even need that condition in the if:elseif part, but it's a place to start. :)

  • Hei, Angie! Since your answer complements mine, move it into my answer. Feel free to improve it. I really prefer to use just the addons I really need, but its good to show all options. – Robson Sobral Jun 13 '13 at 1:21
  • If I could mark 2 answers as correct I would! Not sure which approach I'll use yet, but this gives me something to chew on. Thank you! – hyphen81 Jun 13 '13 at 10:32
  • Hey Robson, no worries! I agree on using just add-ons you need but IfElse is super cool. :) – Angie Herrera Jun 13 '13 at 15:43

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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