301 Redirection in Wordpress, wp_redirect Explained

What is the difference between a 301 and a 302 redirection? In this article I explain how to implement redirections in WordPress through the Dashboard, with few SEO tips.

I’m going to show few ways to implement the function wp_redirect provided by WordPress, supported by Custom Field, Template Page, Post Types and a simple regular expression.

Note. This article is the second part of 301 Redirect Made Simple where different methods are explained, included the .htaccess 301 redirection.

Let's assume that you just managed to re-organize the content of a website and perhaps renamed some categories and tags to reflect the new organization of the content. perhaps you've also moved a page to a different URL. If you setup permalinks (as you should) you have now to fix some aspects related to the SEO of your site.

301 and 302 redirection codes

Mainly there are two ways to notify a robot from a Search Engine that a resource (a page) has been moved to a different URL. The change can be temporary or permanent. The HTTP/1.1 status code 302 represents a temporary redirect, while the HTTP/1.1 status code 301 represents a permanent one. We may use that accordingly.

Both the methods explained in this article require the presence of the former link. If you remove it completely your visitors will land at a missing resource, encountering a 404 error.

So my suggestion is, wherever possible, do not delete entirely the page or the post but copy its content and paste it into a new page. The name of the page itself does not really matter, it just has to keep the original URL.

Which means that you can change the title of the page or post, adding the redirection code at its beginning, like 301 - Original post title here.

wp_redirect

WordPress provides a function for redirecting contents moved to a new URL.

The function is called wp_redirect and requires an URL, and optionally a status: permanent (301) or temporary (302).

From the Codex:

  • $location (required) the URL for the redirection
  • $status (optional) the status, 301, or 302

wp_redirect is a pluggable function. That means, plugins could override it and change the behavior of our codes.

A raw example

Ok, cool... how can we use it?

wp_redirect( $location, $status );
exit;

The exit command is necessary since the function does not exit itself.

We want our visitors to find what they’re looking for, even if they are hitting an obsolete URL. So, if the page has a new permalink, we need to recreate a page at the old URL.

Redirection of a page, via Regular Expression and a proper Template

This method is ideal if you need to move one or more pages, and don't feel like messing up with the .htaccess file.

Create a Template Page within your theme folder. Let's assume you've called it page-redirect-301.php. And inside copy and paste the code below.

<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
  $redirect_301 = get_the_excerpt();
  if ( !$redirect_301 ) {
    print 'Please, provide the new URL of the resource, writing it into the <i>content</i> or <i>excerpt</i> field.';
  } else {
    if ( !preg_match( '/^http:///', $redirect_301 )) $redirect_301 = 'http://' . $redirect_301;
    wp_redirect( $redirect_301, 301 ); exit;
  }
endwhile; endif;
?>

Now it comes the simple part. Edit the page you've just created and copy the new URL of the page you moved and paste it into the content or excerpt field of the page you just created.

Then choose the Page Redirect 301 Template from the Page Attributes Panel.

And that’s all. Save and test.

You can forget the http:// at the beginning of the URL. Do not add other information more than the URL itself, since the page has the mere purpose to redirect to a new URL.

Redirection of a post, using a Custom Field

In case you need to move a post, say you’ve renamed a category, or the title of the post itself changing its permalink accordingly, we can use a Custom Field and a tiny adjustment at the single.php file in your theme.

In the single.php file modify the top lines as shown below.

Remove the following code.

<?php get_header(); ?>

And substitute it with the code below.

<?php
$redirect_301 = get_post_meta(get_the_ID(), 'redirect-301', true);
if ( $redirect_301 ) {
  wp_redirect( $redirect_301, 301 ); exit;
} else {
  get_header(); 
}
?>

Leave the rest unchanged since the same file will be used for all the posts as before, unless you've filled up its custom field with data for a redirection.

On the Custom Fields Panel add a new Custom Field. Call it redirect-301, paste the destination URL, and save.

Give it a test. Opening the old post should redirect you to the new URL even if you click on Preview.