WordPress wp-blog-header.php custom page 404

There’s nasty bugs, then there is this.

A Quick Background

I noticed my website, that I converted to run mostly on WordPress, was dropping rankings in Google. I thought not too much of this, as I figured since a large portion of the site was being served differently, once Google had indexed and caught back up with my new method of madness, I’d climb back up.

I finally decided to go check out Google’s Webmaster tools (which I have set up on the site) to see if it was finding errors I hadn’t seen. Mind you, this is after several months. I quickly noticed something was real-bad-wrong when Google claimed that 70,000 of my links were returning 404’s!!!

So I starting clicking on the links Google claimed were dead. Not a single one of the pages returned a 404, nor showed my custom 404 page I had set up in WordPress. Not a one. Obviously something was still amiss. I snagged the handy Firefox plugin FireBug, and looked at the actual headers that were being returned by those pages. Sure enough. A good portion of my URL’s were giving 404’s. The strange thing, is that the page was returning and being loaded just fine…

After further searching I noticed a pattern. All of the 404 pages were custom pages that weren’t really part of the wordpress framework, but I was including WP functions in the following manner (like the docs suggest you’re supposed to do):


<?php
define('WP_USE_THEMES', false);
include($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');
?>

Look familiar? If you’re reading this article, it probably does. It turns out that this will cause WordPress to throw a 404 header, even though the page displays! Because the actual .php or .html file is in the directory.

I started searching the net, and found several “solutions” to this problem. Some people were even suggesting just changing the title of the pages via filters to override the 404 not found titles!! BAD IDEA. Some were suggesting forcing a new header manually like so: header("HTTP/1.1 200 OK");

I don’t know why it works, but changing the code to include wp-load instead of wp-blog-header seems to fix the 404 error on custom pages! I haven’t found any adverse effects at all…

Here is the fix.


<?php
define('WP_USE_THEMES', false);
include($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
?>

Thanks to the following post (with its own special fix), and more importantly http://www.adrogen.com/blog/wordpress-wp-blog-headerphp-causes-404-in-ie/comment-page-1/#comment-13325 for the final fix.

I wonder how many zillions of people have used this method when using custom pages with WordPress functions included and have no idea that their custom pages are sending 404 headers courtesy of WordPress?

Category: General

Tags: , , ,

- March 15, 2012

Comments

  1. Here, you require some specific function for this website and need to remove some page lines from file path. First, use “wp-config.php” instead of “wp-blog-header.php”. Now check your perfect page location in directory and put it in website.

  2. Thanks man, saved my life!

  3. It seems to only be a problem with certain permalink settings. I believe wp-blog-header.php doesn’t cause a problem for two of the settings I tried… default and /postname/, but when I do /YYYY/MM/postname/, I start to have problems (the problem is somewhere at wp-includes/functions.php:wp(): $wp->main($query_vars);… I think it’s the same problem as http://wordpress.org/support/topic/wp-gtmainquery_vars-not-working so it’s been there for six years). No idea what it is really, but I just wanted to point out it has something to do with permalinks so maybe someone smart can figure out what it is.

  4. Thanks for this. I’ve run into this a few times and have always wound up doing something very hacky and ugly. Glad there’s a quick, easy fix like this.

  5. Howdy, I’m new to running a blog and websites in general and was curious about how you got the “www” included in your domain name? I see your web address, “http://www.ballyhooblog.com/wordpress-wp-blog-header-php-custom-page-404/” has the www and my web address looks like, “http://mydomain.com”. Do you know the simplest way I can alter this? I’m using WordPress platform.
    Thanks a ton

    • Open up the General settings page of your WordPress install where you can change values such as the Site title, Tagline, Timezone, Date Format, Time Format etc. This is also the place where you can change the URL of your blog to have the www included. “WordPress address (URL)” and “Site address (URL)”

  6. Thank you so much! I have been crawling the web for this answer for 6 hours now and didn’t find anything useful. You fixed my issue perfectly; thanks again you are amazing!

  7. Thank you very much. Couldn’t figure this out!

Leave a Reply

Your email address will not be published / Required fields are marked *