WP-Tips: Error 404 Page Not Found Bug!

Jun 9th, 2008 | By dikiem | Category:WordPress Tips and Hack | no comments | 68 views |

I had spent a whole day on the weekend trying to figure out why my .htaccess setup and error 404 is not working on wordpress. After numerous posts reading and blogs reading, I came across a site that perfectly describe the issue. Turn out it is a bug in version 2.5 and higher. This was suppose to be resolved in version 2.5.1, but never did.

It has to do with this page of code inside the ./wp-includes/query.php file.

if ( ('page' != get_option('show_on_front') ) || ( $reqpage != get_option(’page_for_posts’) ) ) {
$q['pagename'] = str_replace(’%2F’, ‘/’, urlencode(urldecode($q['pagename'])));
$page_paths = ‘/’ . trim($q['pagename'], ‘/’);
$q['pagename'] = sanitize_title(basename($page_paths));
$q['name'] = $q['pagename'];
$where .= ” AND (ID = ‘$reqpage’)”;
$reqpage_obj = get_page($reqpage);
if ( ‘attachment’ == $reqpage_obj->post_type ) {
$this->is_attachment = true;
$this->is_page = true;
$q['attachment_id'] = $reqpage;
}

Notice the part in BOLD BLUE. The bug is exactly right there. In line 922 of ./wp-includes/query.php file. By adding an additional “=” resolved the issue. The end result will look like

if ( ('page' != get_option('show_on_front') ) || ( $reqpage !== get_option(’page_for_posts’) ) ) {
$q['pagename'] = str_replace(’%2F’, ‘/’, urlencode(urldecode($q['pagename'])));
$page_paths = ‘/’ . trim($q['pagename'], ‘/’);
$q['pagename'] = sanitize_title(basename($page_paths));
$q['name'] = $q['pagename'];
$where .= ” AND (ID = ‘$reqpage’)”;
$reqpage_obj = get_page($reqpage);
if ( ‘attachment’ == $reqpage_obj->post_type ) {
$this->is_attachment = true;
$this->is_page = true;
$q['attachment_id'] = $reqpage;
}

This will get wordpress to properly recognize improper URL call and redirect the post properly to your 404.php file.

Not sure how to create one? If your theme doesn’t come with one already, you can easily create a very simple 404 error page. Just create a file and name it 404.php. Place that in the same folder as all the other php file of your theme, including the index.php file.

In the 404.php, you could be as basic as this


<?php ob_start(); ?>
<?php header(”HTTP/1.1 404 Not Found”); ?>
<?php header(”Status: 404 Not Found”); ?>
<?php get_header(); ?>

<div id=”content”>
<h1>Error 404: Doh!</h1><br /><br />

</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

You could read up more about it at Wordpress.org or by clicking on this LINK



no comments | 68 views

Tags: , ,



Leave Comment