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-Page doesn't send 404-Headers

November 03, 2012 6:53am

Subscribe [1]
  • #1 / Nov 03, 2012 6:53am

    lehrerfreund

    263 posts

    Hi,

    my site has problems handling soft 404-errors. E.g. if you call
    http://www.lehrerfreund.de/schule/1s/i-am-a-malformed-title-entry-aisjdfiji (there is no entry with the malformed title-entry I use) the user gets correctly directed to the 404-page ( http://www.lehrerfreund.de/schule/404 ), because in my 1s-template I use this code:

    {if no_results}{redirect="schule/404"}{/if}
    {if segment_5!=""}{redirect="schule/404"}{/if}

    also in the htaccess the 404-document is defined as 404:

    ErrorDocument 404 /schule/404

    But as far as I can see the 404-page does not generate a 404-header. Here some shortened headers:

    <a href="http://www.lehrerfreund.de/schule/404">http://www.lehrerfreund.de/schule/404</a>
    
    GET /schule/404 HTTP/1.1
    Host: <a href="http://www.lehrerfreund.de">http://www.lehrerfreund.de</a>
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    DNT: 1
    Connection: keep-alive
    Cookie: exp_last_visit=1036577168; exp_last_activity=1351937847; exp_tracker=a:5:{i:0;s:10:"schule/404";i:1;s:38:"schule/1s/sich-bewerben/3133/undefined";i:2;s:10:"schule/404";i:3;s:38:"schule/1s/sich-bewerben/3133/undefined";i:4;s:10:"schule/404";}; __utma=37294909.360438832.1351938294.1351938294.1351938294.1; __utmb=37294909.8.10.1351938294; __utmc=37294909; __utmz=37294909.1351938294.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __gads=ID=adf4ac625e79d3f7:T=1351938623:S=ALNI_May7GhPPzMp0t1XyOb3ToU3Uz9IEg
    If-Modified-Since: Sat, 03 Nov 2012 10:17:27 GMT
    
    HTTP/1.1 200 OK
    Date: Sat, 03 Nov 2012 10:20:24 GMT
    Server: Apache/2.2.16 (Debian)
    X-Powered-By: PHP/5.3.3-7+squeeze13

    I think the answer should be 404, not 200.

    According to http://joviawebstudio.com/index_ee.php/blog/guide_to_404_pages_with_expressionengine/ (chapter “harden up your soft 404”) I set this code in the very begining of my 404-template:

    <?php
    $this->EE->output->set_header("HTTP/1.0 404 Not Found"); 
    ?>


    I think this should throw a 404, as described in the annotations on the linked page:

    Here’s a little-known gotcha, because we’ve created a 404 page within ExpressionEngine, in some cases, the page technically is not a “page not found”, it is found.  For example, with the technique of requiring entries and embedding the 404 page if no results are found, the server sends out a status 200, or “OK” instead of status 404, “page not found”.  This normally doesn’t matter to the end user, but to search engines and other robots, it means quite a bit.

    The solution is simple here.  You must enable PHP for your 404 template, then place the following code at the top of your 404 page:

    My question is, if there is something wrong in my setup or in my interpretation.

    Thanks in advance!

  • #2 / Nov 05, 2012 5:03pm

    Dan Decker

    7338 posts

    Hi lehrerfreund,

    Have you made sure to set the preference in Design-> Templates-> Global Preferences: 404 Page

    With that set, ExpressionEngine should send the proper 404 header when triggered.

    Cheers,

  • #3 / Nov 05, 2012 5:05pm

    lehrerfreund

    263 posts

    Hi Dan,

    yes, this setting is enabled. Also enable strict URLs is set to yes.

  • #4 / Nov 06, 2012 3:42pm

    Dan Decker

    7338 posts

    Well lehrerfreund,

    It’s time to get a better answer 😉 I’m going to flag an Engineer on this for some insight.

    Thanks for your patience!

    Cheers,

  • #5 / Nov 06, 2012 4:04pm

    Robin Sowell

    13255 posts

    Hi lehrerfreund,

    Let’s see if we can get you squared away.  The redirect tag will typically output a ‘302 Found’ header.  For ‘true’ 404 behavior, what you want to do is

    {redirect="404"}

    That’s what specifically tells EE you want a 404- it will go find the template you have specified as your 404 template and server it up with a 404 header.

    The annotation stuff would be like if instead of redirecting to the 404 page, you embeded the 404 template in a no_results conditional or such.  In which case, EE would treat it like any other embed, and not trigger a 404 header.

    For what you’re doing?  Just try switching out the redirect to be 404 and all should be well.

    Make sense?

  • #6 / Nov 06, 2012 4:34pm

    lehrerfreund

    263 posts

    Ooh Robin,

    again you solved everything with a snip of your fingers.
    Works perfectly - thanks a lot!!!

    BTW: I could have read this in the docs (if no_results).

    (my case closed)

  • #7 / Nov 06, 2012 4:35pm

    lehrerfreund

    263 posts

    Wait, one last question: Is the php-code I mentioned above necessary for the headers?

  • #8 / Nov 06, 2012 4:49pm

    Robin Sowell

    13255 posts

    😉  I could have linked to the docs (and should have)!  LOL- and nope, the php for the headers isn’t needed.  If you were embedding the 404, then yes.  But honestly, that’s an atypical scenario.  The way you have it laid out is very stock and should work w/out php/headers.

  • #9 / Nov 06, 2012 4:54pm

    lehrerfreund

    263 posts

    Yes, headers are nice without it.

    I tried to comment or tell the guys at joviawebstudio (where this hint has been posted) about it, but there seems no way to contact them. Just remember that there are more guys like me running around and pasting this code into their 404-pages.

    (but now, finally: my case closed)

  • #10 / Nov 07, 2012 9:24am

    Robin Sowell

    13255 posts

    It won’t hurt anything to have the headers in there.  And in some more unusual cases, you might need them.  A straight redirect isn’t a problem though.

    Glad you’re all squared away!

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

ExpressionEngine News!

#eecms, #events, #releases