Pages Created Outside of WordPress Throw 404 Not Found Error - June 22, 2008
This site is fairly new and I’m still getting acquainted with WordPress, but so far I like it pretty well. It’s pretty easy to to keep everything valid XHTML with minimal effort. Now, there’s probably another way to handle this, but… I wanted to create a page outside of WordPress, but still keep it “part of the site” with the same style sheets, layout, etc. I created my new contact page and then I borrowed from my WordPress theme by using “require(’../wp-blog-header.php’);” This allowed me to use the get_header() and get_footer() functions to give my new page an identical look to my blog pages even though it was not created from within WordPress. This also made it easier to incorporate a captcha into the page to help keep my email address from getting crawled. Everything appeared to be working fine, but when I used the W3 Validator, it informed me that my server gave it a 404 not found error. This was strange because the page loads just fine. I grepped through the WordPress files for “404″ and found that WordPress likes to throw server codes a lot using status_header(404). Normally, I would think leaving that job up to the web server would probably be preferable, but maybe WordPress has its reasons and to be fair–it probably didn’t count on me creating my own pages outside of WordPress and trying to use the theme files to do it.
The piece of code doing this can be found in wp-includes/classes.php.
if ((0 == count($wp_query->posts)) && !is_404()
&& !is_search() && ( $this->did_permalink ||
(!empty($_SERVER['QUERY_STRING']) &&
(false === strpos($_SERVER['REQUEST_URI'], ‘?’))) ) ) {
$wp_query->set_404();
status_header( 404 );
nocache_headers();
} elseif( is_404() != true ) {
status_header( 200 );
}
I just altered that “if” statement to also check if the REQUEST_URI file or folder exists in the local path. This solved my problem well enough and now the 404 codes for my home-grown pages have disappeared and all is well again. Just replace or set $YOUR_LOCAL_WEB_ROOT with the local path to the root of your web directory. Here is the altered block:
if ( (0 == count($wp_query->posts)) &&
!file_exists($YOUR_LOCAL_WEB_ROOT . $_SERVER['REQUEST_URI']) &&
!is_404() && !is_search() && ( $this->did_permalink ||
(!empty($_SERVER['QUERY_STRING']) &&
(false === strpos($_SERVER['REQUEST_URI'], ‘?’))) ) ) {
$wp_query->set_404();
status_header( 404 );
nocache_headers();
} elseif( is_404() != true ) {
status_header( 200 );
}


