General Understandings about Search Engines
September 11, 2008
Ethical SEO to Improve Search Engine Rankings from a Professional SEO Firm
September 23, 2008

301 Canonical Redirects

A 301 canonical redirect is a way to get all of the web pages on a site to point to a certain address. For instance, if you wanted all of your non www-prefix pages going to pages with a www prefix, you would use a 301 canonical redirect. A more concrete example would be taking the page and redirecting it to This is the 301 redirect part. Canonicalization allows all pages to point to other sites. For instance, a 301 canonical redirect could point all pages with the format to

So why would anyone want to do this? From an SEO perspective, this is a very important optimization technique. Search engines don’t look at non-www prefix websites and their corresponding www-prefix sites as the same. If people place links to each of these URLs then you are in essence building a link campaign for different URLs. 301 redirects centralize these pages and allow all link juice to go to a central page.

There are various techniques to achieving a 301 redirect depending on the type of website or the server the website is on. If the website is hosted on an Apache server, one needs to access a file called .htaccess and modify with the following code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*)$1 [R=301,L]

In most cases this will work. It will not work on a website hosted on an IIS (Microsoft) server, however. If this is the case, one can add the following javascript code to the global header or footer to achieve the same effect:

<script type=”text/javascript”>
var pathd=””;
if (location.hostname.substring(0,3) != ””www””)

But be forewarned. Some search engines such as Google do not understand Javascript and will therefore not help you with you SEO efforts. According to Google Webmaster Help:

“When a redirect link is embedded in Javascript, the search engine indexes the original page rather than following the link, whereas users are taken to the redirect target. Like cloaking, this practice is deceptive because it displays different content to users and to Googlebot…”

If you page is an ASP page, you can use the following redirect code:
If InStr(Request.ServerVariables(“SERVER_NAME”),”www”) = 0 Then
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”,”http://www.”& Request.ServerVariables(“HTTP_HOST”) & Request.ServerVariables(“REQUEST_URI”)
End if

Or the following if it is an ASP.NET page (I’ve found that the redirect only works if interested right after an <html> tag):

<script runat=”server”>
protected void Application_BeginRequest(Object sender, EventArgs e)
HttpApplication app = sender as HttpApplication;
string domainName = “”;

if (app != null)
string host = app.Request.Url.Host.ToLower();
string requestUrl = app.Request.Url.PathAndQuery;

if (String.Equals(host, domainName))
Uri newURL = new Uri(app.Request.Url.Scheme +
“://www.” +
domainName +

app.Context.Response.RedirectLocation = newURL.ToString();
app.Context.Response.StatusCode = 301;

If you want to add code to a .php file, the following should work:

if (substr($_SERVER[””HTTP_HOST””],0,3) != ””www””) {
header(””HTTP/1.1 301 Moved Permanently””);
header(””Location: http://www.””.$_SERVER[””HTTP_HOST””.$_SERVER[””REQUEST_URI””]);


There are several different ways to achieve the 301 canonical redirect and sometimes it takes some code tweaking to get it to work properly, but this is a start.

Get Your Free Website SEO Report

301 Canonical Redirects