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.

Redirect on no_results

June 14, 2011 10:18am

Subscribe [4]
  • #1 / Jun 14, 2011 10:18am

    TomT

    49 posts

    I have the following code in a template:

    {if no_results}{redirect="articles/index"}{/if}

    Where if there are no results for the entry detail I want to redirect the user back to the articles index “channel” or page.

    I keep getting 404’d with this though.

    I’ve tried everything to get EE to redirect the user back to the index template but it only works if I specify an actual channel of the template group like articles/features, can’t get it to redirect to the index.

    _This_ close to just adding a PHP redirect but I figured I’d ask here first.

    Anyone have any idea as to why?

    Thanks.

  • #2 / Jun 14, 2011 11:06am

    Mark Bowen

    12637 posts

    I’m not too sure what you mean by “channel of the template group” and not sure why that’s not working for you as it should be really but have you tried any of these :

    {if no_results}
    {redirect="/articles/index"}
    {/if}

    or

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

    or

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

    Best wishes,

    Mark

  • #3 / Jun 14, 2011 11:14am

    TomT

    49 posts

    I didn’t do a good job of explaining it, sorry.

    I have a template group called “articles”.

    Under articles I have the following templates:
    index: lists all articles
    news: lists all “news” articles
    features: lists all “feature” articles
    detail: the full news or feature entry

    When a user goes to an articles/detail entry that returns no records, I want to redirect them back to articles/index but every iteration of redirect I try gives me a 404, including all the ones you listed above.

  • #4 / Jun 14, 2011 11:34am

    Mark Bowen

    12637 posts

    I didn’t do a good job of explaining it, sorry.

    Actually you did explain perfectly. I’m a bit at a loss as to why what you are trying isn’t working though as it definitely should do.

    Can you get a {redirect=""} without using no_results to work at all? Try one outside the Channel Entries tag and see if it works perhaps.

    Most Important Question
    Next up you do have this {if no_results} conditional placed within the Channel Entries tag don’t you? If not then it will need to be in there in order for this to work.

    After that, do you have any fields being spat out between the Channel Entries Tag Pair so that you can see if you are indeed getting any results or not?

    Hope that helps a bit.

    Best wishes,

    Mark

  • #5 / Jun 14, 2011 11:43am

    TomT

    49 posts

    Can you get a {redirect=”“} without using no_results to work at all? Try one outside the Channel Entries tag and see if it works perhaps.

    It works when I reference a specific channel outside and inside the entries tag but still will not redirect to the index.

    Next up you do have this {if no_results} conditional placed within the Channel Entries tag don’t you? If not then it will need to be in there in order for this to work.

    Yes.

    After that, do you have any fields being spat out between the Channel Entries Tag Pair so that you can see if you are indeed getting any results or not?

    It’s definitely not returning any results.

    The point of this is that I have a custom field with each entry called “suspend_entry” which I use to control author entries (if they break formatting, etc.) so if an entry is suspended (search:suspend_entry=“No” in the channel entries tag) but a user still accesses the articles/detail page I want to redirect them to the index.

    I’m thinking it could have something to do with how I have my .htaccess setup as I have some funky things in there for custom URL functionality for users.

    I guess it’s not a big deal if I can’t get the EE redirect to work, I can just throw a little PHP redirect statement in the conditional but it is a weird problem.

  • #6 / Jun 15, 2011 6:03am

    John Henry Donovan

    12339 posts

    Hi TomT,

    Can you share you full Channel Entries tag please and tell us what your URL looks like on the page you are testing with?

    What version and build of EE are you using?
    Are you using any add-ons?

    If you disable your htaccess and add index.php back into your General Configuration setting does it work?

  • #7 / Jun 15, 2011 9:47am

    TomT

    49 posts

    Here is the channel entries tag:

    {exp:channel:entries channel="features|news" limit="1" search:suspend_entry="No" track_views="one"}

    Sample URL:
    domain.com/articles/detail/full-super-8-trailer-released

    Version: latest
    Add-ons: Wygwam, Hidden Tab and some others.

    Here’s the workaround I wound up using to get the redirect to work:

    {if no_results}
        <noscript><meta http-equiv="refresh" content="0;url=/articles/" /></noscript>
        (opening javascript tag)document.location="/articles/";</closing javascript tag>
    {/if}
  • #8 / Jun 16, 2011 11:33am

    Sue Crocker

    26054 posts

    So you’re OK now?

  • #9 / Jun 16, 2011 11:42am

    Mark Bowen

    12637 posts

    If you are using that javascript to redirect the page then myself personally I would really really want to know why the {if no_results} conditional isn’t working before carrying on any further.

    It may well have implications further on down the line.

    But that’s just me 😉

    Best wishes,

    Mark

  • #10 / Jun 16, 2011 12:18pm

    TomT

    49 posts

    If you are using that javascript to redirect the page then myself personally I would really really want to know why the {if no_results} conditional isn’t working before carrying on any further.

    The conditional is working, it’s the redirect that wasn’t.  Kept giving me a 404 even though the channel was correct.

    So you’re OK now?

    On this issue, yes 😊

  • #11 / Jun 17, 2011 12:33pm

    Kevin Smith

    4784 posts

    Sounds good, TomT! Let us know if you need anything else.

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

ExpressionEngine News!

#eecms, #events, #releases