Okay, so all this redirection business, and the 404 logs it generates, made me really get annoyed with seeing the blank 404 page I have. It's just the basic page that comes with Sandbox. Looking at the page was especially frustrating, since I knew that the URL I was looking at was close to the name of the page I was looking for. Inspired by Smashing Magazine's 404 usability post, I set off to figure it out.
I started with the 404 Search from Weblog Tools Collection, which simply redirects 404 pages to search results. That's not technically correct behaviour, so I borrowed the first few lines of my 404.php from their example:
<?php
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$find = array ("'.html'", "'.+/'", "'[-/_]'") ;
$replace = " " ;
$search_term = trim(preg_replace ( $find , $replace , $search_term ));
$search_term_q = preg_replace('/ /', '%20', $search_term);
get_header() ?>
Then the rest of the 404 template, which you can check out here, follows normally from the sandbox file, until after the search field (which is important for usability). I don't use this in my index or single page templates, because 404's shouldn't land there (if they do, it's a problem with wp_query() ). After the search field, we jump into a custom loop:
<?php /* run the url as a query */
query_posts('s='. $search_term_q );
?>
<?php if ( have_posts() ) :
/* check to see if there are posts, before telling people something that isn't true */ ?>
<div class="content-column left"><h3>You might have been looking for these posts</h3>
<ul>
<?php /* start the loop */
while ( have_posts() ) : the_post(); ?>
<li><strong><a href="<?php the_permalink() ?>" title="<?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></strong><br />
<abbr title="<?php the_time('Y-m-d\TH:i:sO'); ?>"><?php unset($previousday); printf(__('%1$s', 'sandbox'), the_date('', '', '', false), get_the_time()) ?></abbr><br />
<em>
<?php printf(__('Posted in %s', 'sandbox'), get_the_category_list(', ')) ?>
.
<?php comments_popup_link(__('Comments (0)', 'sandbox'), __('Comments (1)', 'sandbox'), __('Comments (%)', 'sandbox')) ?>
</em>
</li>
<?php /* end the loop */
endwhile; ?>
</ul></div>
<?php /* close the list before closing the ifelse */
else : endif; ?>
You can read the comments, but basically, we take the URL and use query_posts() to run it as a search, and return the results as a list, if there are any. So far, it works for posts that I've referenced elsewhere, but it's not returning the actual post itself. That's probably because I use category slugs in my URL's. and BTW, the fancy code highlighting in this post is courtesy of a fancy new plugin.
UPDATE: Some additional code was used from Arun's nearly perfect 404.









8 Comments
This is incredibly clever, Adam. I’m definitely going to use this. It’s an excellent point: is a 404 page any better than just search results with a 404 message? Probably not.
as far as using this yourself, do beware. i’m pretty sure there’s supposed to be some
preg_replace()happening so that it returns results for the terms in the address, not the address itself. i’ll update the post when i get my head around it.yep. Digging through the comments on the WLTC post I found that Arun already figured this all out. the appropriate PHP magics have been added to the post.
ETA: i fixed the .htaccess problem that was making me see so many 404′s. thusly, the link in the post now takes you to the post, instead of a 404. you can use this link instead: http://archgfx.net/blog/wank
Excellent! This is just what I was looking for
I’ve implemented in my site (http://www.seotier.com/404page). Thank you very much
Simple but amazing! Just added it to a site I am working on finishing up that really needed this to fix some missing posts that have been moved around.
I struggled with a few 404 WordPress plugins but they were all horrible.
This works great. Thank you!
This no longer works on my WP 2.9 blog. Will there be an update?
It appears to still be working on this blog (2.9). I haven’t had much time to poke around with it though.
3 Trackbacks/Pingbacks
04 Oct 2007 at 3:31 pm
archGFX | Dream in Infrared 0.6 for WordPress 2.3
[...] in Infrared 0.6 is done, which adds compatibility with WordPress 2.3′s native tagging. It also adds search results to 404 pages, as well as a list of recent posts. There’s also a bug fix, so that the Table of Contents [...]
05 Nov 2007 at 4:32 pm
En bedre tegneserieside, del 1: 404 not found at superevil.org
[...] syntes at jeg var færdig, da jeg faldt over denne artikel – søgeresultater inkluderet på 404, gættet ud fra den URL man har forsøgt at tilgå? Nice. [...]
23 Mar 2009 at 7:21 am
Make WordPress search more useful | Brass Blogs Web Design
[...] Honestly, this is something I wish I knew where I got the code from. Yet again, I didn’t note the source of this code, so I’m going to start off by telling you that I did not come up with this on my own – I found it somewhere, a long, long time ago in a galaxy far, far away. So if anyone happens to know where I did get this code from, please feel free to let me know and I’ll credit the true genius who came up with this. Actually, it came from Adam over at archgfx. [...]
Post a Comment