Friday, March 02, 2012

How To Set Up A Proper 404 Error Page On Your WordPress Blog

How To Set Up A Proper 404 Error Page On Your WordPress Blog:

The humble 404 has been with us since the tubes that make up the internet were first plumbed in. Websites can actually return a whole smorgasboard of different error codes, but 404 is the most common and generally referred to as “not found“.

Sometimes your browser will handle these and return a generic error page, but because WordPress infers content from the URL dynamically (as opposed to there being an actual file on the server), it handles errors internally too.

Unfortunately, the default message is pretty much as unhelpful as you can get, so today I’m going to show you how to tweak your WordPress 404 error page to be a whole lot more useful.

DIY or Friendly 404 Messages

You may have noticed we have our own custom error page at MakeUseOf which is somewhat more welcoming than the default message. To make your own custom error page, simply create a template called 404.php in your theme folder – if you don’t already have one that is. Duplicate the single page template if you like, and cut out the main content area. You can then either hand code an image like we have, or write your own simple message. Then you start adding some advanced functionality.

Email You About the Error

Usually when someone hits a 404 error page, it’s because they’ve followed a bad link somewhere rather than typing a silly URL directly into the address bar (though, never underestimate their ability to do that too). In those cases, it would be helpful for you as the admin of the site to know about the error, and either correct your own bad link or to ask the site linking to you to correct it. Paste this code snippet in full to the very top of your shiny new 404.php page:

<?php $message = "";
if(isset($_SERVER['HTTP_REFERER'])):
$message .= "User came from: ".$_SERVER['HTTP_REFERER']."\r\n";
endif;
$message.="Page URL they tried to access was: ".$_SERVER['REQUEST_URI']."\r\n";
$admin_email = get_option('admin_email');
@wp_mail($admin_email,"404 error",$message);
?>

The above code will send an email to the registered admin email address describing the URL they came from, if available, and the URL they tried to get to. On the user side, it does nothing – it only emails you, and doesn’t output anything onto the error page itself.

Add a search form

The user obviously came to your site looking for something, so rather than leave them frustrated, let them search for what it was they wanted instead. To do this, find a suitable place on your template, perhaps add a little note about “Why not try searching instead?”, and paste in this single line of code:

<?php include(TEMPLATEPATH . "/searchform.php"); ?>

Plugins to Enhance the Error Page

If custom coding a 404 page isn’t really your thing, then no worries because there’s a few plugins that can give you a better 404 experience too. Let’s take a look:

404 Redirected

This plugin monitors your site for 404 errors and records them so you can be alerted from the admin panel. Moreover, if you find some external site has linked to you with the wrong link, you can redirect it without having to contact them – just tell the plugin where that broken link should be going, and visitors will be sent right along with a search engine friendly 301 automated redirection.

Smart 404

Even better than trying to fix the 404 errors as they popup would be to never show them in the first place. Smart 404 does exactly that – it looks at the URL, extracts keywords from it, and tries to figure out exactly what the user was trying to go to. There’s no screenshot to show you, because it doesn’t need any configuring and just works out of the box. The downside is of course that if the URL contains nonsense keywords, it’s unlikely to find any matches – so spelling mistakes are also out.

Anyway, I hope you decide to make a proper WordPress 404 page, either DIY style or with a plugin. Do you know of any other clever 404 related plugins you’d like to share, if so then let us know in the comments! We also got a rapidly growing archive of fairly advanced WordPress tutorials, as well my own Getting Started with WordPress free guide – which covers everything from essential first steps and plugins all the way to monetizing and SEO; and be sure to check out the Best of WordPress Plugins page.






No comments:

Post a Comment

[Please do not advertise, or post irrelevant links. Thank you for your cooperation.]