Ryan Jones Blog – dotCULT.com Ryan Jones Blogs About Internet Culture, Marketing, SEO, & Social Media

November 28, 2011

How To 301 Redirect A Website

Filed under: Main — Ryan Jones @ 3:37 pm


How do I 301 Redirect my website? I get asked this question more often than I should, so instead of constantly answering it I’ve decided to create a blog post I can point people to. But first, let’s start off with some background.

Why Would I Want To Redirect A Page?

There’s several valid reasons to implement a 301 redirect. Maybe you just want to move a page do a new domain. Maybe you’re combining two pages into one and need to redirect one of the URLs. Maybe you have a vanity URL that you’re using in TV or Radio because it’s smaller. If you’re taking down last year’s product, it’s a good idea to redirect the page to a newer version instead of returning a 404 not found. In fact, anytime you take down content I’d look for a place to redirect that’s helpful to the user.

So what does 301 mean anyway?

301 is the HTTP status code for a permanent redirect. The official definition of a 301 is as follows:

“The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.”

In layman’s terms it basically tells a search engine that “Hey, this page has moved to this new address. Update your records accordingly.” It also allows the search engines to properly transfer any trust or ranking signals associated with the old domain to the new one (if they so choose)

301 is the preferred method of redirection for SEO. Sadly, the default method on most IIS web servers though is a type 302. 302 returns a status of Found(Elsewhere). Realistically it should only be used when the webserver is doing it due to some hiccup, and generally never on purpose.

Wait I thought 302 meant temporary? You’re right, it did. It’s been replaced with a 307 redirect now. A 307 redirect tells search engines that “this is temporary, please re-visit the orginal URL the next time you come calling.” With a 307 or 302, no pagerank/trust/authority is passed to the resulting location. In other words, this is bad. There’s also a hardly used type 303 redirect which says “go to this other location, but use a GET request instead of a POST.” If done right, you shouldn’t ever need to use 303. (If you’re still interested in the differences, here’s a great status code pamphlet by Sebastian.

Ok, so how do I implement a 301 redirect?

There’s several ways to do a 301 redirect. .htaccess is the easiest but we’ll also cover how to do it at the page level, as well as how NOT to do it. We’ll use the basic example of redirecting the non-www version of your website to the www version. So for the examples we’ll be redirecting http://example.com to http://www.example.com

301 redirects with .htaccess

If you’re running Apache and can edit your .htaccess file this is the simplest way. Just put the following in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

301 redirects in PHP

In some cases, you can’t do it with .htaccess so you’re stuck doing it at the page level. If you need to do a 301 redirect in PHP, here’s how:

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.example.com/”);
?>
Note: you’ll need to make sure you don’t echo out any HTML or text before executing these functions.

301 redirects in ASP

If you’re using ASP, it looks like this:

<% Response.Status = "301 Moved Permanently" Response.AddHeader "Location", "http://www.example.com/" Response.End %>

301’s in Python

If you’re using Python you’re already geeky enough that you don’t need me to tell you, or you work at a Google. But here’s how to do it anyway:

from django import http
def view(request):
return http.HttpResponsePermanentRedirect (‘http://www.example.com/’)

301 Redirects in IIS

If you’re using IIS I pity you, but it’s still possible to do a 301 redirect. You just need to pay attention to detail and make sure you check that box that says permanent. Here’s a good guide to configuring IIS redirects. Here’s another step by step guide with screenshots.

How NOT to Redirect

No primer would be complete without telling how NOT to do it so you can avoid these SEO death traps.

Don’t Use JavaScript Redirects
JavaScript redirects aren’t guaranteed to be crawled by search engines and may not work with all users. Here’s what they look like so you can spot them.

<script type=”text/javascript”>
window.location.href=’http://www.example.com/’;
</script>
note: they could also use document.location or document.url in place of window.location.href but those are depreciated or don’t work in all browsers.

Don’t Use META refresh tags either.
Meta refresh tags are even worse than JavaScript ones, and they look like this:

<meta http-equiv=”refresh” content=”0;url=http://www.example.com/”>

And that’s all there is to it. Happy redirecting.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress