How to setup a 301 Redirect

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.

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/page.html");
exit();
?>

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 http://domain.com version to http://www.domain.com.

<?php
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']);
}
?>

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/http://www.domain.com/newpage.htm

Apache .htaccess Canonical Redirect

Follow the same steps as before but insert the code below instead (it will redirect all the visitors accessing http://domain.com to http://www.domain.com)

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

ASP Single Page Redirect

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

<%
Response.Status="301 Moved Permanently"
Response.AddHeader='Location','http://www.new-url.com/'
%>

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
%>

You can receive our articles for free on your email inbox, with more web design, SEO, monetization and blog tips. Just enter your email below:

61 Responses to “How to setup a 301 Redirect”

  1. Lionel Young on February 10th, 2007 9:35 am

    Sometimes you need to create a dummy file if you want to do a permanent/temporary redirect. When I switched over to Wordpress, I had to redirect index.html to index.php, but the .htaccess in your root folder will affect subfolders — this means your add-on and subdomains.

    All my add-on domains were redirecting to the root domain, and had to create a dummy index.html in the root folder for the HTTP_HOST condition to trigger.

    #Redirect only for elegantcab, and not add-on domains
    RewriteCond %{HTTP_HOST} ^(www\.)?elegantcab\.com
    RewriteRule ^index.html$ http://www.elegantcab.com/index.php [R=301,L,N]

    So if you’re having problems, try creating a dummy file.

  2. Daniel on February 10th, 2007 10:55 am

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

  3. Bes Z on February 10th, 2007 4:21 pm

    Sorry if this is a repost, for some reason I pressed the “Say it!” button and I got refreshed and sent back to this page and couldn’t see my comment [you can delete my earlier comment if it goes through].

    Nice work, Daniel. :) I am going to use it as one of my cheat sheets now.

    For WordPress users who change their permanent links [permalinks] and wish to use 301 redirects to point users, search engines and others to the new urls, Dean Lee created a migration plugin which does just that:

    http://www.deanlee.cn/wordpres.....on-plugin/

    Of course, it only works on WP pages and not pages created outside of WP.

    Thanks for sharing. :)

  4. Enblogopedia on February 13th, 2007 1:43 am

    I want to change my permalinks from
    http://www.enblogopedia.com/2007/02/10/what-ever/
    to:
    http://www.enblogopedia.com/what-ever/

    I believe the code will be something like this:

    RewriteEngine On
    RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.+) http: //www.enblogopedia.com/$1

    (of course with out the line break!)
    Am I right?!

  5. Daniel on February 13th, 2007 9:45 am

    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.

  6. Enblogopedia on February 13th, 2007 10:04 am

    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

  7. Affiliate Manager on March 17th, 2007 3:37 pm

    For the most part I’ve been trying to make sure all of my sites just have the redirect for http://www. to the non http://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.” :)

  8. bohemianbiz on April 2nd, 2007 2:12 am

    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.

  9. praguebuzz on April 2nd, 2007 9:14 am

    i used your technique .htaccess. It worked FOR A WHILE. Occasionally, it doesn’t let me access http://www.praguebuzz.com/blog/. When I remove the .htaccess, it started working again. I put back the .htaccess, I could access the blog section, but then acted out later. The cycle continued. For now I removed the file.
    Here is what I put in this file.

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

    thank you

  10. Mexxep on April 18th, 2007 2:54 pm

    test

  11. 局域网监控 on May 3rd, 2007 11:55 pm

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

  12. FilterInternet on May 3rd, 2007 11:58 pm

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

  13. Ranjana on June 20th, 2007 2:23 am

    Hi,

    We have several domains pointing to the same web site.

    For instance - http://www.abc.com, http://www.xyz.net,
    http://www.efg.co.in

    Our main domain name is - http://www.aaa.in

    All the above mentioned domains and the main domain point to the same server… as in
    there is only one folder for these domains.

    How do I incorporate .htaccess in this case?

    I mean I do not have separate folders for any of these domains but I want that if any one

    types in “http://www.abc.com”, it should get resolved to “http://www.aaa.in”.

    One of the domains is very old and is registered in search engines. I do not want to lose out on that. Am desperately looking for a solution to this problem.

    I hope I have been able to explain the situation properly.

    Thanks.

    Regards
    Ranjana

  14. Daniel on June 20th, 2007 9:02 am

    Renjana, if http://www.abc.com is pointing to http://www.aaa.in, people will already get forwarded there, there is no need to setup a 301 in that case.

  15. Ranjana on June 21st, 2007 8:59 pm

    Hi Daniel,

    The problem is it does not get resolved to http://www.aaa.in, it stays as http://www.abc.com

    As a result “http://www.abc.com” is still in the search results and “http://www.abc.com” is not getting the required exposure.

    How do I tackle that?

    Thanks.

    Regards
    Ranjana

  16. TechZilo on July 12th, 2007 2:12 am

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

  17. Daniel on July 12th, 2007 2:19 am

    Which one, the PHP Canonical redirect?

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

  18. TechZilo on July 12th, 2007 2:34 am

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

  19. Daniel on July 12th, 2007 4:52 am

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

  20. TechZilo on July 14th, 2007 12:32 am

    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?

  21. Daniel on July 14th, 2007 2:56 am

    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:

    http://www.dailyblogtips.com/t.....s-plugins/

  22. TechZilo on July 14th, 2007 3:20 am

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

  23. Daniel on July 14th, 2007 8:14 am

    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.

  24. Tricia on August 3rd, 2007 4:48 pm

    I’m using a 301 redirect on my main blog feverishthoughts.com to include the http://www. The problem is that I have a few other blogs on this domain that I put in directories ie feverishthoughts.com/oddplanet. 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 feverishthoughts.thewebfiles.com/oddplanet

    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?

  25. Outlaw on August 20th, 2007 3:33 am

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

  26. Jerry Peters on September 5th, 2007 5:44 pm

    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.

  27. Ashwini on September 30th, 2007 1:33 am

    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?

  28. Chris on October 14th, 2007 2:01 pm

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

  29. MaiURL on October 31st, 2007 12:01 pm

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

  30. cachiladomr on November 10th, 2007 1:27 pm

    ricacelcaal

  31. Gilbert on December 5th, 2007 3:46 am

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

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

    Response.AddHeader=’Location’,'http://www.new-url.com/’ won’t work

    Response.AddHeader ‘Location’,'http://www.new-url.com/’ will work

  32. Tom - Graphic Design on February 29th, 2008 8:09 am

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

  33. shekhar suman on April 2nd, 2008 6:56 pm

    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???????????????

  34. Ashfame on June 11th, 2008 9:17 am

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

  35. isaac on June 19th, 2008 2:23 pm

    do the redirecting through the net for me

  36. SEO Genius on June 21st, 2008 2:54 pm

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

  37. Internet Marketing Leeds on July 2nd, 2008 4:47 pm

    ..really useful and informative post.
    I have just successfully redirected a site by ammending a code that wasn’t working due to an error…thanks for sharing.

  38. Get a free website on July 3rd, 2008 8:40 pm

    thanks a lot for this tip. Will use it on all of my sites from now on.

  39. graphic art on September 4th, 2008 4:15 am

    I tried doing it. But after several minutes my server went down. Internal server error. I removed the .htaccess file and it goes up again.

  1. WWW or no-WWW?
  2. Guide To Setting Up 301 Redirects | Connected Internet
  3. Blogger outputting bad Atom feeds with invalid MIME types - Peter Cooper
  4. links for 2007-03-09 | Bloganbieter.de Blog
  5. fiLi's tech
  6. Best Wordpress Plugins: Enforce www. Preference
  7. ceronueve » Blog Archive » with or without www
  8. www.inmydreamland.com » with or without “www”
  9. fioricet
  10. FlasheR
  11. Blog Setup: 40 Practical Tips
  12. ultram ultracet
  13. Mix All Information URL » Blog Archive » How to setup 301 redirect ?
  14. 301 Redirect and htaccess for WordPress | Magic In The Desert
  15. Importance of Backing Up Blog Regularly | MT Herald Dot Com
  16. GizmoTech » Blog Archive » 301 redirect
  17. WWW. or Not and Where Is My PageRank? « Hello World!
  18. Colorado Small Business Blogging » Blog Archive » Not Redirecting Your Web Pages is Just Plain Rude
  19. Supercow.net » How to setup a 301 Redirect of a domain
  20. WWW. or Not and Where Is My PageRank? | Bohemian Journey
  21. 301 Redirect for SEO | John A Simpson
  22. Importance of Backing Up Blog Regularly

Got something to say?





Sponsors

Online Invoicing For Freelancers Premium WordPress Themes Why I recommend Doreo Hosting Maximize Your Rankings Smarter blogging

Popular Articles

Recent Articles

Killer Domains eBook