ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

if no_results 404 redirect problem

September 19, 2011 7:01pm

Subscribe [2]
  • #1 / Sep 19, 2011 7:01pm

    richardmeredith

    48 posts

    Hi, I’m trying to stop non existent urls in a blog.

    I’ve got the following code in a blog template:

    {exp:channel:entries channel="{blog_channel}" limit="1" dynamic="no" sort="asc" disable="trackbacks|comments|categories|member_data|pagination|category_fields" require_entry="yes"}
    {if no_results OR segment_3 !="" }
    
            {redirect="404"}
    
        {if:else}
    
    Stuff here
    
        {/if}
    
    {/exp:channel:entries}


    The segment 3 part works fine, but the no results doesn’t seem to work. If I throw in a non existent url: http://www.domain.co.uk/blog/nonexistenturl it still shows one of the other posts?

  • #2 / Sep 19, 2011 7:42pm

    Mark Bowen

    12637 posts

    Hi Richard,

    I’m pretty sure that the {if no_results} conditional is quite a simple one and so can’t be used with an extra conditional mixed in with it.

    I believe if you use two separate conditionals then hopefully this will work for you.

    Mark

  • #3 / Sep 20, 2011 4:58am

    richardmeredith

    48 posts

    Hi Mark,

    Ok that works, noticed i also had dynamic=“no”

    Just wondering how would I get this to work on the entry template that wouldn’t have url_title.

    http://www.domainname.co.uk/blog

    On this page I have 2 or 3 of the latests post but getting 404. I guess because it doesn’t have a url_title in the url?

  • #4 / Sep 20, 2011 12:34pm

    richardmeredith

    48 posts

    Hi Mark got it sorted, just wanted to ask if this was an efficient way of doing it. Looking to keep page load as quick as possible.

    {exp:channel:entries channel="{blog_channel}" limit="1" sort="asc" disable="trackbacks|comments|categories|member_data|pagination|category_fields"}
    {if segment_2 == ""}
    CONTENT HERE
    {/exp:channel:entries}
    
    {if:else}
    {exp:channel:entries channel="{blog_channel}" limit="1" sort="asc" disable="trackbacks|comments|categories|member_data|pagination|category_fields" require_entry="yes"}
    {if no_results}{redirect="404"}{/if}
    OTHER CONTENT HERE
    {/exp:channel:entries}
    {/if}

    Thanks

    Rich

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases