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.

404 Question

October 03, 2008 5:22pm

Subscribe [3]
  • #1 / Oct 03, 2008 5:22pm

    Nora

    41 posts

    I’ve been browsing through the forums and other sections but couldn’t find an answer to my question. I’ve already set up a 404 page in case a post isn’t found but I was wondering if there was a way to display a 404 error page in case the template url isn’t correct such as:
    mysite.com/site/sometemplate/somethingelse so that it displays a 404 page when sometemplate or somethinelse doesn’t exist. I did read somewhere that this isn’t possible with EE but I’m curious if someone found a way around it. 😊

  • #2 / Oct 03, 2008 8:04pm

    Ryan Irelan

    444 posts

    Hmm. That should work. What do you see when you try it?

  • #3 / Oct 04, 2008 2:33am

    Nora

    41 posts

    It doesn’t..
    I have a template named content and a page to show posts named article.
    It will only work if I add a number after article like this:
    somesite.com/content/article/1/ but when I have a misspelled post it will just show the last post in that category. It also doesn’t show a 404 for templates (not template group) that don’t exist.

  • #4 / Oct 05, 2008 5:17am

    Jesse B.

    33 posts

    Hi Nora!

    404 pages are only triggered automatically when the template group doesn’t exist.

    Because of the dynamic nature of EE URLs, there’s no built-in logic for displaying a 404 when a particular entry doesn’t exist. Instead, you need to use {redirect="404"} in your templates when you’ve determined that you have no matching entries to show.

    For more information, please see this blog entry.

  • #5 / Oct 05, 2008 5:20am

    Nora

    41 posts

    Thanks for the reply. I’ve already done that and it works fine but I’m trying to find a way to show a 404 page when the template within the template group doesn’t exist. Hope that makes it a little clearer. 😊

  • #6 / Oct 05, 2008 5:38am

    Jesse B.

    33 posts

    Yikes, you did say that. Sorry.

    Could you use that logic in your template group’s index template? If you’re not expecting to pass in specific parameters to that template, you could test whether you have too many URL segments, meaning that ExpressionEngine couldn’t find the requested template and defaulted to index instead.

    This is just a guess, because I don’t have a copy of EE handy.

  • #7 / Oct 05, 2008 5:44am

    Nora

    41 posts

    Sorry not sure what you mean.
    When a template doesn’t exist it does redirect to the index of the template group but that would give search engines the idea that the page exists and is a duplicate of the index. I’ve already had a lot of trouble with search engine rankings since I switched over to EE and at Google I was told that the wrong system results could be causing it.

    I haven’t played around with the Query features yet but would checking the database to see if a template within a template exists help solve this?

  • #8 / Oct 05, 2008 7:00am

    Nora

    41 posts

    This appears to be working when segment_2 doesn’t exist:

    {if segment_2 != ''}
    
    {exp:query sql="SELECT template_id FROM exp_templates WHERE template_name = '{segment_2}' & group_id = '3' "}
    
    {if no_results}
    This template doesn't exist
    {/if}
    
    {/exp:query}{/if}

    I’m still checking if everything’s correct before I redirect it to my 404 page but so far it’s giving the right results. 😊

  • #9 / Oct 05, 2008 9:02am

    dwex

    94 posts

    Have you looked at the blog entry that Jesse B linked to? I think you need to make use of the ‘require_entry’ tag and do something like:

    {exp:weblog:entries require_entry="yes" rdf="off" limit="1" disable="categories|custom_fields|member_data|pagination|trackbacks"}
    {if no_results}
    {if segment_2}
    {redirect="404"}
    {/if}
    {/if}
    {/exp:weblog:entries}

    at the top of your index page, which is somewhat hackish because of the extraneous queries involved. I’m using something similar.

    This can also probably be done with .htaccess, more efficiently, but the host I’m on doesn’t let me touch .htaccess, so I have to do wierd hackish stuff in my index template 🐛

  • #10 / Oct 05, 2008 9:21am

    Nora

    41 posts

    Thanks 😊
    I read the blog but I believe that’s to check if there are entries.. would that work with checking templates too?

  • #11 / Oct 05, 2008 10:12am

    dwex

    94 posts

    The no_entries condition would trigger if there are no entries. If there is a segment_2, it would fire the redirect, so the template would only display if there were no segment_2. If you want to allow certain values of segment_2, you could use an advanced conditional to control when the redirect fires.

    Again, I do something similar because I have no access to .htaccess.

    But I’m pretty sure the best way to do this would be via .htaccess, if you can do that.

  • #12 / Oct 05, 2008 12:10pm

    Nora

    41 posts

    Thank you so much 😊
    I’ll go try it out now.

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

ExpressionEngine News!

#eecms, #events, #releases