By Darrin J. Ward

I'll preface this refresher on 301 and 302 HTTP redirects by saying that we always strive to plan the layout of Websites so that we will never need to move or rename pages. You should try to do the same!

However, sometimes it's unavoidable and pages need to be moved or removed entirely. When that happens, it's very important that the right strategy be used to "redirect" the page from the old URL to the new URL. There are two viable options for doing page redirects: a 301 redirect or a 302 redirect. The 301/302 number refers to the "status code", which is sent by the Web server to robots/crawlers/browsers, informing them of what action is being taken.

A 301 redirect code indicates that the move is PERMANENT. This type of redirect should be used when you know that the page WILL NOT move back to its original location.

A 302 redirect code indicates that the move is TEMPORARY. This type of redirect should be used when you know that the page WILL eventually move back to its original location, or somewhere else.

In the vast majority of cases, it's the 301 redirect that you will want to use. The 301 redirect passes most of the PageRank and "link juice" from the old page address to the new page address, which is exactly what you want to help maintain rankings and PageRank. I will point out that there is going to be some loss due to the 301 redirect (our internal research suggests anywhere from 10-25% is lost), which is why it's best to avoid redirects altogether, where possible.

302 redirects do not maintain the PageRank and link juice in the same way.

Implementing 301 and 302 Redirects

The implementation of these redirects is usually done at either the server level or in the programming code. Here are some samples.

301 Redirect in PHP

I wrote the following simple function to allow me to perform 301 redirects:

function Redirect301($GoTo) { header("HTTP/1.1 301 Moved Permanently"); header("Location: $GoTo"); }

This function can be called in your php code like this:


302 Redirect in PHP

PHP has a built-in function called redirect() which does a 302 redirect. You can call redirect() like this:


301 Redirect in Apache using .htaccess or httpd.conf

Using the HTTP server itself is a popular way to perform redirects. Because I use Apache most of the time, I'll limit the discussion to the .htaccess and httpd.conf methods for Apache, and leave IIS and other servers alone for now. The easiest way to perform a 301 redirect is to use the Redirect directive, which can be used like this in .htaccess or httpd.conf:

Redirect Permanent /old-page.htm /new-page.htm

302 Redirect in Apache using .htaccess or httpd.conf

302 redirects also use the Redirect directive, but without the Permanent flag. i.e:

Redirect /old-page.htm /new-page.htm

Testing Redirect Response Codes

You should always check that the server is sending the correct redirect response code. Don't just assume that your redirects are working correctly. Here are a couple of tools that I recommend for testing HTTP redirects (and server headers in general):