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):
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.
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?