How to Use 301 and 302 Redirects Efficiently in WordPress : Whenever you request a webpage, the server sends back what is known as a “response code”. This quote is a predefined number that provides us with information about the status of the request. If everything went as expected, the server responds with the code number 200. If the page was not found, it sends back a 404. If there was an unspecified server error, a response code of 500 is generated.

In the case of a redirect, the server sends back one of several codes depending on what happened. Most redirect codes are either a 301 or 302. It’s important to know the difference between them so that you can configure your website to return the proper code depending on your intention.

301 Redirects

You should use a 301 redirect when you have permanently moved your content to a new URL. Say for example you wrote a post with a certain URL slug. Your readers have shared it with each other and you’ve even received a link from a website. Unfortunately you realize that the URL is not ideal. Either you made a spelling mistake or there is some other issue. Changing the URL however would mean that you lose whatever activity has been generated for your existing one. What you want is for people to still be able to make use of the old URL but get redirected to the new one seamlessly and without any action on their part. In this situation, you would use a 301 redirect.

Or perhaps you moved your entire site to a new domain. If you had your old site for a long time, there would be a lot of “link juice” pointing to it from sites all around the Internet. In addition, search engines would have fully indexed your old content. When you move to a new domain, you don’t want to lose all of that. Ideally, you would like to tell the search engines “Hey look! I just moved my stuff to a new URL permanently. I’d appreciate it if you carried over the reputation of my previous pages to the new ones.” A 301 redirect will send exactly that message.

How to Use 301 and 302 Redirects Efficiently in WordPress

302 Redirects

If on the other hand a new URL is temporary, you should send back a 302 response code instead. One common use for 302 redirects is for website maintenance. If you have to put your website offline while you perform some maintenance tasks, you can temporarily redirect all your pages to a page that says “Website is down for maintenance”.

When using a 302 redirect, search engines will not remove the old URL from their index and they won’t index the new one either.

Redirection Techniques

Here are three ways you can implement redirects on your WordPress site:

1. Using .htaccess

Your .htaccess file is a powerful configuration tool for your entire website. It’s way beyond the scope of this article to explain the various things you can do with it, but you can easily configure a redirect. Simply use the keyword “Redirect” followed by three parameters. The first is the response code, the second is the old URL, and the third is the new URL. So if you want to redirect /index.html to /home.html, here is what you

Redirect 301 /index.html
Redirect 302 /somepage.html

You can also use the “RewriteRule” directive to achieve the same result:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index.html [R=301,L]
RewriteRule ^somepage.html [R=302,L]
RewriteRule ^somepage.html [R=302,L]

Remember – modifying your .htaccess file is dangerous if you don’t know what you’re doing!

2. Using PHP Redirects

This is an easier (and in many ways safer) technique. With a few bits of PHP code, you can redirect your users to any webpage. Just make sure that there is nothing before the following code. No HTML, no line spaces, and no blanks.

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yournewsite/index.php");

Simply replace http://www.yournewsite/index.php with the website to which you wish to redirect your user. Alternatively, if you need to do a 302 redirect, simply remove the first line:

header("Location: http://www.yournewsite/index.php");

3. Using WordPress Plugins

This is the easiest and most error-free way to implement redirects in WordPress. If you don’t want to muck around with PHP or .htaccess, or you don’t have the knowledge to do either, this is the way to go. The exact plug-in to download will depend on your needs.

For example, if you want to redirect all “not found” pages back to your homepage, you can use this simple plug-in here:

There is also this Redirect plugin that allows you to build a list of 301 redirects: