301 Redirects from your Wordpress 404 Error Page

by Terri Ann on November 30, 2007

If you’re are doing any change to your Wordpress blog where you need to redirect old page URL’s to new ones try this on for size.

If the page no longer exists it will re-direct to your 404 error page.

We are going to put some code at the very top of your 404 error page that will detect and redirect the URL’s we need to redirect.

Array of pages to redirect

First I made an associative array of the URL’s I need to permanent redirect somewhere else.

$redirect_301 = array(
    '/2007/11/02/old-page/' => '/2007/11/01/new-page/',
    '/2007/10/02/old-pg/' => '/2007/10/02/new-page/'
);

Find out what page the browser is trying to locate

Then we’ll set the variable request equal to the page requested by the browser

// If you're looking for http://blog.example.com/2007/22/02/old-page/
// $request will = '/2007/22/02/old-page/'
// Just like what we have as the index for our array above
$request = $_SERVER['REQUEST_URI'];

If the page is the in the array, redirect it!

Then a simple if statement checks to see if $request exists as a index of the array and if it does it passes a 301 header and redirects the browser.

if (array_key_exists($_SERVER['REQUEST_URI'], $redirect_301)) {
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: http://blog.ninedays.org' . $redirect_301[$request] );
    die();
}

Then you would proceed with your error page for the rest of the 404 error s that doesn’t need to be re-directed.

Full Sample Redirecting Wordpress 404 Error Page

<?php
    $redirect_301 = array(
        '/2007/11/02/old-page/' => '/2007/11/01/new-page/',
        '/2007/10/02/old-pg/' => '/2007/10/02/new-page/'
    );
    $request = $_SERVER['REQUEST_URI'];

    if (array_key_exists($_SERVER['REQUEST_URI'], $redirect_301)) {
        header("HTTP/1.1 301 Moved Permanently");
        header('Location: http://blog.ninedays.org' . $redirect_301[$request] );
        die();
    }   

    // back to wordpress
    get_header(); ?>
<div id="content"> 
  <h1>
    Error - This page no longer exists
  </h1>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Why would I use this

I have mistakenly changed dates on posts and had people linking to both, this way both links point to the correct article without changing any of the links. Using the 301 redirect Google and other search engines can follow to the correct page, eventually filtering the incorrect path out of their index.

{ 2 comments… read them below or add one }

1 Navin Poeran January 22, 2009 at 3:34 pm

Thank you for this nice article. I tried it, and it works, the only problem I’m getting now is: new “error” pages are not giving the 404 page, instead I’m getting this error:

Parse error: syntax error, unexpected TCONSTANTENCAPSEDSTRING, expecting ‘)’ in /home/blablabla/publichtml/navinpoeran/wp-content/themes/typoxp-2.0/404.php on line 5

An “else” statement is missing?

Thank you in advance.

2 Navin Poeran January 22, 2009 at 3:40 pm

Ah, I’m so dumb, I forgot to put comma’s. It’s working fine now, thanks a lot :D

Leave a Comment

Previous post:

Next post: