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
Web designing
March 21, 2012 - 5:33 am
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.
Lautaro
September 13, 2012 - 12:59 pm
Thanks man, saved my life!
Lee-kai
November 30, 2012 - 4:02 pm
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.
Ryan
March 16, 2013 - 9:16 am
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.
no
May 28, 2013 - 9:17 am
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
Ballyhoo Blog
May 28, 2013 - 12:48 pm
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)”
Devon
June 13, 2013 - 2:59 pm
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!
Austin
April 1, 2014 - 4:22 pm
Thank you very much. Couldn’t figure this out!