How To Fix Broken Links and 404 Error Page Redirects in Wordpress

Wordpress now has the ability to redirect old article links and titles to the new correct page, provided you’re using the latest version of Wordpress, but what about 404 pages?
When a link is truly broken, and Wordpress can’t already fix it automatically, you might as well just deal with the visit to your site as a 404 error page. There are plu gins out there that require manual input to redirect traffic, but is the broken link getting so much traffic that you need to go to all that trouble rather than adding content to your site. The article location will be re index in the search engines anyway, and you shouldn’t spend all your time being a perfectionist when it isn’t going to get you much of a ROI.
Since the visitor will land on a 404 error page that doesn’t monetize, or provide anything of interest for the visitor, it seems like a good idea to redirect, or forward, the visitor to the site’s homepage where they might find something of interest.
Wordpress uses a standard 404.php landing page for every theme. The code usually looks something like this:
<?php /** * @package WordPress * @subpackage Default_Theme */ get_header(); ?> <div id="content" class="narrowcolumn"> <h2 class="center">Error 404 - Not Found</h2> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
To redirect visitors to your homepage, so they don’t land on a 404 error page, lose interest in your site and leave, we’ll start by creating a file to add to your theme templates.
Name the new file header404.php.
Copy the code from your header.php template file, and paste it into your new header.php file.
Now we’ll add the code we need to redirect our 404 error page visitors to our homepage.
At the very top of the file we’ll add this PHP code:
<?php
// 301 Moved Permanently
header("Location: http://www.nerdgrind.com/",TRUE,301);
?>
Change nerdgrind to your domain name.
We’ll also add a second line to be sure the web browser redirects in case the first line fails for some reason:
<meta http-equiv="refresh" content="0; url=http://www.nerdgrind.com/" />
The code will look something like this in the header404.php file:
<?php
// 301 Moved Permanently
header("Location: http://www.nerdgrind.com/",TRUE,301);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>;
charset=<?php bloginfo('charset'); ?>" />
<meta http-equiv="refresh" content="0; url=http://www.nerdgrind.com/" />
The rest of the code will come after the meta tag we are using to redirect, but overall this will be a standard header.php file with our new lines placed as you see them here.
Next we’ll need to edit the 404.php template file.
In order for the 404.php file to call our new header404.php file we’ll need to replace:
get_header();
with:
<?php include('header404.php'); ?>
To see this script in action try typing in an article link on NerdGrind that doesn’t exist like:
http://www.nerdgrind.com/noarticle-here
With your 404 error pages redirecting your traffic to your homepage you can rest assured you won’t lose any traffic from broken links that land on a 404 error page. The best 404 error page is the one visitors don’t see.
Related Posts:
One Response to “How To Fix Broken Links and 404 Error Page Redirects in Wordpress”
Leave a Reply
© Copyright Nerd Grind 2009 - 2010. All rights reserved.

Thanks so much for this code! HELPS A LOT!