IE and Custom Error Pages

An oft overlooked part of one's website is the 404 page. Many of the visitors who see your 404 page will most likely be first time visitors looking for something in particular. Rather than show them some drab looking "404 Page Not Found" message you might want to consider providing something a little more useful. For instance, my 404 page contains this site's archives - listed both monthly and topically as well as a short message about what just happened. I also included a few suggestions in addition to my contact info. Your 404 page should be given the same attention as your home page. Consider it the gateway to your site.

The problem is, although all modern browsers will display your custom 404 page, Internet Explorer may not. Since v5, IE insists on using it's own built-in error page. There are some people who claim that upon arriving at a non-existent URL Internet Explorer will quickly size up your custom error page and if it is less than 512 bytes in total size it will render its own error page. The solution then seems obvious - ensure your custom error page is greater than 512 bytes. Sounds good but it is not foolproof as I found out. As I said earlier, I have plenty of information on my error page and at one point even included a bunch of nonsensical text commented out in the source code which guaranteed the page was greater than 512 bytes yet IE still at times insisted on using its own error page. It was hit and miss.

So how then do we force IE to use your custom error page? Add the following code to the top of 404.php,

<?php ob_start(); ?>
<?php header("HTTP/1.1 404 Not Found"); ?>
post
About Len Kutchma

Len has been blogging for over 10 years and is a rabid WordPress fan. In addition to blogging here you can find him writing the occasional article and toiling away in the forums at WeblogToolsCollection.com. He also hangs out at the WordPress support forums lending a hand where he can. Be sure to follow @wpcanada on Twitter.

Comments

  1. meowmie says:

    I searched high and low for a solution to this problem (and tried everything from making sure that I have 512 bytes to adding a line in the .htaccess file), but none of it worked until I came upon your post. Thank you so much!

  2. Alex F says:

    Thank you very much for sharing the helpful tip.
    I have been searching for a solution and it worked perfectly.

  3. Jake says:

    Thanks for the pro tip - I was having this same issue. It seems a little odd, though. Why does IE8 want this separate header information?

  4. Captain Errorpage says:

    thanks for the solution, it seems IE still behaves however it wants to even in 2010! thankfully people like you are on hand to help out! :)

  5. Thanks so much! I was completely baffled by this issue. Firefox kept reading my WordPress custom 404 properly but IE8 just ignoring it over and over. I tried everything I could think of and nothing worked until adding those couple php lines and voila. It works!

  6. Cees says:

    Too bad the trick doesn't seem to work outside of WordPress (??).

    I added the extra code to my custom 404 page, which works splendid in FF, but gets ignored by IE8....

    Weird he?

  7. Ned says:

    Woo hoo - thanks heaps - I had the same problem - and your solution works well for me in IE8 - using WordPress 3.0.1 - I've passed the solution on to my theme forum and you are now bookmarked on my computer as a wordpress guru!

    • Len Kutchma says:

      Hi Ned! Happy to hear this post was useful to you.

      WordPress guru? Me? No. :) Just someone who loves the software and learned (and continues to learn) by trial and error.

      Check out my page called World of WordPress to see some real gurus - particularly Jean-Baptiste of Cats Who Code and Leland of ThemeLab. (my personal favourites)

  8. Rich says:

    I pasted the code at the top of my WP 404 page...cleared memory and cache.. zip. Still not working in IE.

    Any thing else I'm missing?

    In IE8, go to to http://www.jazzradionetwork.com/ and click on Jazz Lovers in the body copy below the menu

    Rich

  9. clay says:

    I have been pulling hair (not mine - think I'm crazy?) out trying to get my custom 404 PHP scripts to play nice with all browsers, and as soon as I get one browser working, the others aren't happy or I end up having to make some worthless compromise.

    This page gave me such hope, but unfortunately, it doesn't work for me. I still get the regular generic IE 404 page when directing to a PHP file, but not HTML.

    Any thoughts?

    Happy New Year!
    Clay

  10. Aaron says:

    Thanks so much! I can confirm this works even outside of wordpress. I use dreamweaver and added it to the very top and it works amazingly now in IE. I was at wits end just like everyone else here and happened to come across this on google. Thanks again!

    • Len Kutchma says:

      You're welcome Aaron. Glad it worked for you. This bugged the crap out of me for a long time as well. :)

  11. Willem says:

    I was looking for hours and tryed everything. Your soultion for IE did the Job. Thank you so much. !! ;-)

  12. Micke Varg says:

    Hello
    Tried this trick but it doesn't work, might be that I have soe other issue, sofar I have found that in the menu (and only on IE) it adds /// to the http:, I have those empty so people can choose the subpages - in every other browser than IE this works just fine. However I don't get the 404 even if entering a page that doesn't exist on the site, just the generic one from IE.

    I have added the path to the 404 in .htaccess so the error might be here also, any ideas on how to get this working?

    //Micke Varg

    • Len Kutchma says:

      Hi Micke.

      I can't seem to reproduce the error - it works for me. Sounds like something is interfering with it. What it could be is anyone's guess.

      • Len Kutchma says:

        Okay, now I see what you mean. You're using the WordPress custom menus and have left the parent page's URL blank. Since the URL is blank IE is using its own error page since the theme cannot deliver one. (the URL is blank) I'm not sure of a solution for this. I'll have to do some research.

        • Len Kutchma says:

          Hi Micke.

          The only solution I can come up with at this time is to remove the http:// from the meta box leaving the URL field blank. Take a look at this linked image.

          This will result in the parent page not being clickable at all while the child pages will be.

Please Note: WPCanada is a moderated community. Please read the Comment Policy.

What Do You Think?

*