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

featured wordpress 404 error page   How To Set Up A Proper 404 Error Page On Your Wordpress BlogThe 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.
makeuseof wordpress 404 error   How To Set Up A Proper 404 Error Page On Your Wordpress Blog

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.

404 error wordpress page with redirected   How To Set Up A Proper 404 Error Page On Your Wordpress Blog

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.

The comments were closed because the article is more than 180 days old.

If you have any questions related to what's mentioned in the article or need help with any computer issue, ask it on MakeUseOf Answers—We and our community will be more than happy to help.

6 Comments -

0 votes

Money Saver

Great article! I am working on my own 404 page, our blog is fairly new but I have made a ton of adjustments (built on wordpres) and am learning a lot from sites like yours, thanks for that!

Mike from Moneysaver Blog http://www.moneysavermag.com/blog

0 votes

Elga

Some of the newest WordPress themes have this search function in 404 pages already set up. Mine has it and I’m glad. I need to work out a little bit on the design. 404 pages have to be nice as the rest of my website.
Very useful info here. Thanks for putting it down for us!

0 votes

Edz

Hi, does this will help to prevent 404 error in Google Webmaster? I hope you’ll reply on my comment

0 votes

James Bruce

No, this won’t prevent 404 errors Edz. To do that, you would need to log the errors and fix them in the first place.

0 votes

karthik chandrappa

Will multiple 404 related plugins conflict ech other?

0 votes

James Bruce

Depends on the plugin in question. If you have one to log a 404, and one to automatically redirect, i dont suppose they would conflict. Obviously ,two plugins designed to redirect a 404 wont work.