How to setup a 301 Redirect


background image

The “301 Permanent Redirect” is the most efficient and search engine friendly method for redirecting websites. You can use it in several situations, including:

  • to redirect an old website to a new address
  • to setup several domains pointing to one website
  • to enforce only one version of your website (www. or no-www)
  • to harmonize a URL structure change

There are several ways to setup a 301 Redirect, below I will cover the most used ones:

PHP Single Page Redirect

In order to redirect a static page to a new address simply enter the code below inside the index.php file.

header("HTTP/1.1 301 Moved Permanently");

PHP Canonical Redirect

The Canonical 301 Redirect will add (or remove) the www. prefixes to all the pages inside your domain. The code below redirects the visitors of the version to

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

Apache .htaccess Singe Page Redirect

In order to use this method you will need to create a file named .htaccess (not supported by Windows-based hosting) and place it on the root directory of your website, then just add the code below to the file.

Redirect 301 /old/oldpage.htm /new/

Apache .htaccess Canonical Redirect

Follow the same steps as before but insert the code below instead (it will redirect all the visitors accessing to

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^ [nc]
rewriterule ^(.*)$$1 [r=301,nc]

ASP Single Page Redirect

This redirect method is used with the Active Server Pages platform.

Response.Status="301 Moved Permanently"

ASP Canonical Redirect

The Canonical Redirect with ASP must be located in a script that is executed in every page on the server before the page content starts.

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("SCRIPT_NAME")
End if

Browse all articles on the SEO category

52 Responses to “How to setup a 301 Redirect”

  • SEO Genius

    Yey 😀 been wanting to know how to do this for a while now

  • isaac

    do the redirecting through the net for me

  • Ashfame

    Thanks Daniel for the article and the help via IM. Got it done through .htaccess file.

  • shekhar suman

    well recently we moved to a new domain frm googlepages free server, now the worst part is googlepages doesnt support any of the methods stated above, only option left is meta refresh and loose my pagerank, any other solution i could not find till now, can any one of you help me out???????????????

  • Tom – Graphic Design

    Thanks for the useful info! I used your advice to easily setup a 301 on our design site!

  • Gilbert

    There’s an error on the ASP single page redirect.

    The line that reads Response.AddHeader= should not have the =

    Response.AddHeader=’Location’,’’ won’t work

    Response.AddHeader ‘Location’,’

  • cachiladomr


  • MaiURL

    thanx bro.. i search many at google.. but your tips is better..

  • Chris

    Great article!
    But I do have a question about the www or no www
    Where exactly do I add this code? .htaccess file?

  • Ashwini

    I have a question.
    I currently work on blogger domain and will soon shift to a custom domain.301 redirect will be enough to transfer the google juice but does the same goes for Technorati?

  • Jerry Peters

    The ASP Canonical Redirect code above is for redirecting the non-www domain name to the www domain name. What is the code if you are going the opposite direction–redirecting from the www domain name to the non-www domain name?

    Also, I assume the code is placed at the top of every page before the and tags. Is this correct?

    Thanks in advance for your help on this.

  • Outlaw

    Very interesting info. However did anyone have any experience with trailing slashes at the end of subdirectory on your domain. For instance: and
    how are these treated? I saw some of people having problems with that and both pages indexed. any htaccess ideas on that?

  • Tricia

    I’m using a 301 redirect on my main blog to include the www. The problem is that I have a few other blogs on this domain that I put in directories ie I want the directory blogs to have urls that do not contain the www because they have higher PR without the www in their urls, yet the main domain has a higher PR with the www in it’s url.

    So I’ve been using the nowww plugin on my directory blogs to keep the www out of the urls. I suspect this might actually be causing a bit of a problem on a few of the blogs because now they occasionally have the primary domain thewebfiles in their url as well if it’s accessed without a trailing slash. for example the Odd Planet blog sometimes converts it’s urls to

    I’d like to use an htaccess redirect to keep the www out my directory urls so I could avoid using the plugin. I’ve also read that the plugin might be using more cpu resource in my account and I’ve just had a terrible experience with web hosts and cpu resource over use so I want to trim down.

    Do you have any idea what htaccess redirect I might use to solve my problem?

  • Daniel

    TechZilo, maybe it is something related to your host then.

    The easiest option now is just to go to Google Webmaster tools, verify your blog and choose a “Preferred Domain”, that way Google at least will know what version it should use.

  • TechZilo

    I tried the plugin,but it took down the blog.
    i tried the php one, but it didnt work. can you explain in detail?

  • Daniel

    Did you try the PHP one? If the PHP words there is no need to use an .htaccess redirect. Also there is a plugin that does this job, check this post:

  • TechZilo

    I don’t have an htaccess file now, and need to upload one. My FTP client, fireftp, doesnt allow upload of .htaccess file

    I tried uploading a txt and renaming it to .htaccess
    it didnt work either.

    how can i set up htaccess redirect now?

  • Daniel

    Put it in the beginning, but it should not matter.

  • TechZilo

    Yes, Canonical redirect. Where in header? at the beginning, middle or end?

  • Daniel

    Which one, the PHP Canonical redirect?

    This one go on the header.php file on your theme.

  • TechZilo

    I couldn’t understand the second method, ie non-www to www redirect. Where should I place the code? .htaccecss?

  • FilterInternet

    What should I do if my virtual host provider do not configure IIS to do redirect for me? My host use html+asp.

  • 局域网监控

    靠,虚拟主机提供商不帮我做301重定向怎么办? 我的空间是html+asp。

  • Mexxep


  • praguebuzz
  • bohemianbiz

    wow, many many thanks. I used the .htaccess way, and it worked like a charm. now i understand why sometimes I see a google page rank of 4 and sometimes 0 on a site of mine. those nasty “wwww” makes huge difference.

  • Affiliate Manager

    For the most part I’ve been trying to make sure all of my sites just have the redirect for www. to the non www. For one, it’s a lot easier and quicker to type in the non “www” and if I’m going to submit my sites to hundreds of directories, it can save a lot of time.

    So, I personally go with non “www.” 🙂

  • Enblogopedia

    Well I think it`s the easiest way,
    check this article. I don’t mess with my file templates..and I think the code is true but I want the “OKAY” from a prof. that`s all. :s

  • Daniel

    Enblogopedia, I am not sure about changing the URL structure with .htaccess. I tried it sometime ago with PHP, but it was a mess.

    I will let you know if I find something online.

  • Daniel

    Lionel, that is a good way to solve the problem of redirecting single files without affecting the whole website, thanks for sharing.

Comments are closed.