0

I'm using this code for my 404 redirect:

{exp:channel:entries channel="about-us" limit="1" require_entry="yes" {gv_param_disable_default}}
        {if no_results || last_segment != url_title }

        {redirect="404"}

        {/if}
<!--content here-->     
{/exp:channel:entries}  

Why wouldn't this work? It seems to stop an additional segment from being added to the url, but if I just change the last segment to an invalid path, it just goes to the blank index template for that channel.

  • I've tried to go with an if statement of {if segment_3 != ""} instead. The odd thing is the no_results if statement works fine, and the segment_3 if statement works fine, but if I try to put them together, no matter what operator I use, it doesn't seem to want to behave correctly. So for the time being I just have 2 if statements for my 404 redirect. Anyone have a better idea? – hyphen81 Feb 7 '13 at 12:13
  • Could you provide a little more background on your setup? Are you using Structure? Is the URL to this page a template? Is there anything else on the template that would ever show if there are no results? I'm trying to understand why EE's 404 settings wouldn't accomplish what you need. – Mark J. Reeves Feb 7 '13 at 13:40
2

The only thing you normally need for 404 with a require_entry="yes" and strict urls enabled is:

{if no_results}{redirect="404"}{/if}

Can you just try this on a clean template? As others have said, a broader context could help debugging this.

EDIT: if, as per your comment, you know you should only have two segments, then simply modify the code as follow to redirect to 404 if more segments are added

{if no_results OR segment_3 != ""}{redirect="404"}{/if}

Have a look at this little blogpost on 404 pages in ExpressionEngine for more detailed information.

| improve this answer | |
  • Yes I'm using EE 2.5.3. I'm not using structure. With only the code you mentioned, it does work. But only if I change segment 2 to some invalid value. If I add a 3rd segment, rather than redirect, it just displays the index template for the current channel. So I've currently got website.com/index.php/about-us/pastors-welcome. If I say about-us/pastors-welcome-cheesy-grits, it redirects. But if I say about-us/pastors-welcome/cheesy-grits, it just displays pastors welcome – hyphen81 Feb 8 '13 at 12:00
  • I actually tried your code suggestion originally and it wouldn't work like that. It seems to work fine if I seperate the two conditions into 2 seperate if statements, which is what I ended up doing to get it to work, but I just thought there must be a better way to do it. – hyphen81 Feb 8 '13 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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