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 such a thing in the Dashboard. With few SEO tips. Never let your visitors clueless when they’re looking for a resource that has been moved. 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.

So you managed to re-organize the content of a website, or named differently categories and tags. Or just moved a page at a different URL or under a different parental page. If you instructed WordPress to use permalinks (as you should), all those re-arrangements can cause trouble for a SEO perspective and, most important, to your visitors.

We need to do something about, don’t we?

Say also that you are not exactly craving to put your hands into the .htaccess file. Or that you want to give to your clients the possibility to administrate such a potential issue via Dashboard. Right! Let’s see how we can deal with all of those in a simple ways.

First, what’s the difference between 301 and 302?

Mainly there are two ways to advise a robot from a Search Engine that a resource has been moved. The mutation can temporary or permanent. The HTTP/1.1 status code 302 represents a temporary redirect, while the HTTP/1.1 status code 301 signals a permanent one. We may use that accordingly.

Let your visitors land somewhere, anyway

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, to not delete entirely the page or the post but to copy and paste its content. Sometimes it’s easier to recreate a page, at the previous URL. In this case, the name of the page itself does not really matter, till it has the old URL as its permalink.

In any case, I would give to the page (or post) we use for the mere redirection a name like 301 redirection to New name of the page. Or 302 redirection… etc. This way, I will remember in future what’s this page or post is about.


WordPress provides a proper function that can take care of moved links. It accepts various parameters, let’s give a look.

The function is called wp_redirect and requires an absolute URI (an URL, it’s basically the same thing). More than the URI (the destination to where you would like to redirect your visitors), the function accepts a status: permanent (301) or temporary (302).

From the relative page on Codex these are the parameters we can provide to the function:

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

Be aware that wp_redirect is a pluggable function. That means, plugins could override it and change the behavior of our codes. This the case, you would eventually like to implement your own function like for instance the one explained in 301 Redirect Made Simple.

A raw example

Ok, cool… how can we use it?

Well, we can just give the command that simple.

The function takes care of all the aspect of the redirection, saving you trouble. The exit command is necessary since the function does not exit itself.

How can we make a good use of it?

To make an efficient use of the function via Dashboard we need to implement a way to provide wp_redirect the parameter via the Page Editor or the Post Editor. The two cases can be different. I’m going to show ways to redirect for both, saving the SEO value of the previous page. If you’re in a hurry, you can jump to one of the two parts of the article:

Save the value of the page, for the SEO sake!

First, and I’m repeating it here, we need to keep in mind that all our efforts would be useless if we don’t let visitors and Search Engine’s robots find anything at the previous URL. That’s true specially for pages (while it seems that WordPress can take care of posts when we change their permalink).

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 a page, or many, and you don’t want to get mad learning how to not mess up with the .htaccess file.

Simply create a Template Page in your Theme. Call it for instance page-redirect-301.php. Inside copy and paste the following.

< ?php
	Template Name: Page Redirect 301
	File Name: page-redirect-301.php
	Redirect a page using wp_redirect
	the status is 301, moved permanently
	Recreate a page at the old URL and 
	write in the content of the page the URL 
	of the new resource.
	The resource will be permanently moved
	at the new URL, saving the SEO value.

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 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;

You don’t need more. Now it comes the simple part. Copy the new URL of the page you moved and paste it into the content or excerpt field of the page you just created for the pure redirection. Like below.

wp_redirection explained, copy the new link in the content

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

wp_redirection explained, select the page template

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. But after all, why should you? 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 first lines the way below.

From this:

To this:

ID, 'redirect-301', true);
if ( $redirect_301 ) {
	wp_redirect( $redirect_301, 301 ); exit;
} else {
} ?>

Leave the rest unchanged since the same file will be used for all the posts as before. From now on, it will check if you associated with the particular post a Custom Field for the redirection. If not, it will execute the loop as normal.

Get the Custom Fields Panel and add a new Custom Field. Call it redirect-301, fill it up with the new location of the post (its new URL), and save.

301 Redirection Made Simple, setting the Custom Field

Give a test: opening the old post should redirect you immediately to the new URL even if you click on Preview.

Conclusion and SEO aspects

When redirecting a resource, a page or a post the main thing is to not let your audience feeling lost. It’s a good practice to provide them a way to reconnect to what they are looking for. They may have bookmarked a page of yours. You moved it, and they need to find where it is now.

Regarding the SEO, the correct way it’s to advise a robot from a Search Engine that the resource has been moved permanently (or temporarly with the 302 code, in place of 301), communicating to it the new URI.

This way we save the rank (or value) of the page. Search Engines will visualize the new URI in the result page when people will search for our content.

Nicely, if an user bookmarked a page (or a post) of your website, opening that old URL will not result in an unpleasant 404, but it will effectively redirect to the actual location.

Let me know if you ended up with a better way to do it, and in case, let’s make it even better.




2 thoughts on “301 Redirection in WordPress, wp_redirect Explained

  1. Thank you Nick, indeed that’s a good point. The solution worked months ago for a customer of mine, so I did not see that problem coming. I will revise this post as soon as I’m done with a project I’m working on these days.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>