In Search of the Perfect 404 pt 2
- Posted On: March 23rd, 2007
- Filed Under: Local Stuff
- Tagged As: How To
About a month back I wrote a short post about creating a more functional 404 page. At the conclusion of that post I hinted at the fact that I'd be writing again about a special addition to the 404 page that might make it a bit more functional. I thought now would be a good time to fulfill the promise I made back then.
Occasionally visitors come looking for content that's not where it used to be. Maybe it was deleted, maybe it was moved, maybe it was lost - who knows. As I mentioned in the previous post it sometimes helps to add things like a tag cloud, a more pronounced search bar or even a plugin like Landing Sites to treat your users to another way of locating content on your site.
Recently I added something new that I thought I'd share. While it's nice to be able to find content on your current site, if you've removed content (for whatever reason) it may pay to direct your users to the Google Cache.
Why the Google Cache?
Before you start worrying about sending visitors away from your site, consider losing potential return visitors who are turned off by a 404 error. Because Google caches your entire page, including sidebar, a good deal of your navigation structure can be maintained inside the cached page. Your RSS feed, archive links, and search bar will still be valuable links even if they're inside a Google cache frame.
Visitors who turn around and leave (because they can't find your content) don't get to see any of this.
Creating Dynamic Links to the Google Cache
Let me start by saying that this is pretty specific to WordPress. The core PHP code can be used in other PHP environments but I'll be talking specifically about implementing it into WordPress.
There are really only two files that you'll need to edit. While you can certainly create a plugin for your WordPress theme I recommend just editing your functions.php file and your 404.php file. Before we jump into editing the code, though, I should point out that there's one hurdle we're looking to jump.
While WordPress can tell you the URL of any given post, it can't tell you the URL of a post that no longer exists. Because Google needs the URL where your content was originally found, and WordPress doesn't have that info, we can't rely on existing functions like "the_permalink()" to work. Luckily, when your server generates a 404 error it pushes your 404 template to the address of the page that can't be located.
To jump that hurdle we just use some PHP to grab the current URL. The code we're going to use is borrowed from WebCheatSheet.com it grabs the URL of the current page for use on the 404.php file. Simply take the code below and paste it into your functions.php file.
Editorial line breaks are marked with **
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":". ** $_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
echo $pageURL;
}
Next we need to incorporate the function into our 404 template.
Setting up your 404
The function above will return the URL which generated the 404, our next step involves turning that URL into a search on Google Cache. I've done the hard work here and dissected the referrer string. Your goal is just to put the function into the 404 template in the right place. In my case I just created a link leading to the following URL:
Editorial linebreaks are again marked with **
http://72.14.209.104/search?hl=en&q=cache%3A **
<?php curPageURL(); ?> **
&btnG=Google+Search
You can see how I use that in my 404 page. It's actually at the base of the main paragraph.
Remember that this link will only work if the page was cached. It's not a foolproof solution to retrieving deleted content, just another thing that you can add to your arsenal.
Closing Notes
Because of the nature of this topic, and the code involved, I'm curious to know what you thought of it. If you've got any additional questions, any comments, or ideas that could better this post please let me know. I'd be glad to build on it or expand it if people are at all interested. Thanks,
5 Responses to “In Search of the Perfect 404 pt 2”
-
March 24th, 2007 at 11:53 am
This is great. I’ve always wanted a more functional 404 page. I’m going to try this out as soon as my connection normalizes. Thanks man.
- Brown Baron
-
March 24th, 2007 at 11:57 am
Hey Jeremy, thanks for your comment. I was actually just reading your post on Optimizing WordPress for Search Enginges great stuff!
- WildBil
-
March 24th, 2007 at 12:02 pm
Brown Baron,
Let me know how it goes! There are tons of creative 404 pages out there, I think Jeremy’s is pretty cool too.
I’m thinking about adding something else to my 404 soon (something more fun) and I’ll be sure to update about it. Again, let me know how yours comes out!
- WildBil
-
March 24th, 2007 at 12:08 pm
[...] You can read the secon part of this series at In Search of the Perfect 404 pt 2. [...]
- In search of the ‘Perfect’ 404 pt 1. | Bill2me dot Com



I like to be creative with my 404.
http://www.jemmille.com/404
I really like your advice and I use WordPress so this is beneficial to me. Nice post.
- Jeremy