• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
Daily Blog Tips

Daily Blog Tips

How to make money from your blog

  • Home
  • Popular
  • Contributors
  • About
  • Contact / Advertise
  • Blogging
  • Marketing
  • Design
  • Money
  • Reviews
  • Productivity
  • Software
  • Technology
  • Guides

How to setup a 301 Redirect

By Daniel 50 Comments Reading Time: 2 minutes

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.

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

Daniel Scocco

About Daniel

Daniel Scocco is a programmer and entrepreneur located in São Paulo, Brazil. His first company, Online Profits, builds and manages websites in different niches. His second company, Kubic, specializes in developing mobile apps for the iOS and Android platforms.

Filed Under: Marketing

Reader Interactions

Comments

  1. Togrul says

    at

    Thanks for such an informative post.

    It helped me much.

    Cheers,
    Togrul

    Reply
  2. Chris Lang says

    at

    Forgot to mention that you have done a great service to all blog users and that you have great resource on your site.

    Thank you and best wishes to you.

    Reply
  3. William Summers says

    at

    Great Tutorial. The PHP code worked great but I am having some serious issues with the ASP code for another site. When applied to the default.asp page I get the following error, Microsoft VBScript compilation error ‘800a0400’. So I removed the code so the site will work.

    Any ideas will be greatly appreciated.

    Reply
  4. Udegbunam Chukwudi says

    at

    Thanks for a nice tutorial. The PHP Single Page Redirect just came in handy as I created a custom 404 page for my blog and main site. 😉

    Reply
  5. Chris Schwarz says

    at

    Hi,

    I am bringing in in a new website and plan on setting up 301 redirects in the .htaccess file.

    Do I need to keep the OLD .html pages on the server?

    Reply
  6. Shannon - SKS Designs says

    at

    I am currently using the following permalink structure and am looking to change it through a HTACCESS 301 redirect but cannot find the code that works.

    Currently permalink: /%year%/%monthnum%/%postname%.html
    Future permalink: /%postname%

    So, I want to remove the year and month from all posts and well as the .html ending.

    Can someone help me?

    Reply
    • Udegbunam Chukwudi says

      at

      @Shannon – SKS Designs: I suggest you install Scott Yang’s Permalink Redirect plug-in. I use it on my blog and it works like a charm 😉

      Reply
  7. pozycjonowanie says

    at

    Moved permanently is big part of SEO. It’s elementary know-how every SEO Specialist.

    Most important implementation in link building:

    * allow to collect linkjuice

    Reply
  8. Golland says

    at

    After hours on the phone with technical support it seems my hosting company won’t support the use of 301 re-directs.

    My site is on a Windows server and there doesn’t seem to be any way to make it work.

    Now looking for a new host that uses Linux servers and has better technical support!

    Reply
  9. Alexs says

    at

    After surfing online for an hour, I finally found an answer to my problem, tried it out and URL redirection worked like a charm. Unlike other sites I found, your article is concise, less words and straight to the point.

    Some readers might find this helpful, if your launching a new website or blog, plan from the beginning if you’re going to use the root of your website or if you’re going to use a subfolder. I installed my blog (wordpress) to a subfolder, and eventually changed my mind and want to move my blog to the root directory. Fortunately there is this URL redirection thing.

    Now I can finally use my main domain without typing any additional subfolders.

    Nice article thank you!

    Reply
  10. Email Tracking says

    at

    I have done canonical redirection in ASP.

    Google is treating my main page as “/” and also as “default.asp”

    I mean when I log in to webmaster tools, google is saying that I have a duplicate content on two pages and pages are “/” and “default.asp”

    Can anyone tell me why is it doing so?

    Reply
  11. wie bali says

    at

    Thank you for the tip 🙂 You’ve inspired me to bring my .htaccess file up to speed.

    Reply
  12. Mohamed Beshr says

    at

    very good and i will apply some of them to my site

    Reply
  13. Sameer Arora says

    at

    hi,

    this is really a nice URL

    I wonder if anyone could advise me on how to setup a 301 redirect page using html ?

    I have some URLs with .htm extensions indexed by Google
    I can only see php and asp stuff here

    Reply
  14. Bang Kritikus says

    at

    Thanks for your nice tips, Daniel.

    Reply
  15. Tarik says

    at

    When I use the .htaccess method, it redirects all my non-www traffic to the homepage. That’s a big mistake.

    I recommend using the PHP direct. Much cleaner. No .htaccess files to deal with, plus it’s a pitch and paste coding task. Redirect 100% better.

    Thanks for the tips, Daniel.

    Reply
  16. Dave Slade says

    at

    Hi

    Hey guess what i found out : Did you know that you can manually choose the domain page by going to googles webmaster page and chose the settings tab.

    Reply
  17. Gary Conner says

    at

    The code to go the other direction is above under “PHP Canonical Redirect”.

    Thanks again!

    Reply
  18. Hendry Lee says

    at

    If you can avoid PHP or other backend level redirection if you have access to .htaccess. Not only will it be faster because you don’t have to start PHP every time someone enters or visit your blog or site with the “wrong” canonical name.

    Code-level redirection is necessary only if it is application specific, or not as permanent.

    Reply
  19. graphic art says

    at

    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.

    Reply
  20. Get a free website says

    at

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

    Reply
  21. Internet Marketing Leeds says

    at

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

    Reply
  22. SEO Genius says

    at

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

    Reply
  23. isaac says

    at

    do the redirecting through the net for me

    Reply
  24. Ashfame says

    at

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

    Reply
  25. shekhar suman says

    at

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

    Reply
  26. Tom - Graphic Design says

    at

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

    Reply
  27. Gilbert says

    at

    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’,’

    Reply
  28. cachiladomr says

    at

    ricacelcaal

    Reply
  29. MaiURL says

    at

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

    Reply
  30. Chris says

    at

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

    Reply
  31. Ashwini says

    at

    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?

    Reply
  32. Jerry Peters says

    at

    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.

    Reply
  33. Outlaw says

    at

    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?

    Reply
  34. Tricia says

    at

    I’m using a 301 redirect on my main blog feverishthoughts.com to include the 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?

    Reply
  35. Daniel says

    at

    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.

    Reply
  36. TechZilo says

    at

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

    Reply
  37. Daniel says

    at

    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:

    Reply
  38. TechZilo says

    at

    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?

    Reply
  39. Daniel says

    at

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

    Reply
  40. TechZilo says

    at

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

    Reply
  41. Daniel says

    at

    Which one, the PHP Canonical redirect?

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

    Reply
  42. TechZilo says

    at

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

    Reply
  43. FilterInternet says

    at

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

    Reply
  44. praguebuzz says

    at

    Reply
  45. bohemianbiz says

    at

    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.

    Reply
  46. Affiliate Manager says

    at

    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.” 🙂

    Reply
  47. Enblogopedia says

    at

    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

    Reply
  48. Daniel says

    at

    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.

    Reply
  49. Daniel says

    at

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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular

  • 28 Ways to Make Money with Your Website 514 Comments
  • 43 Web Design Mistakes You Should Avoid 474 Comments
  • 4 Steps to Increase Your Blog Traffic 188 Comments
  • How to Find Advertisers for Your Website 126 Comments
  • Top 25 SEO Blogs 243 Comments
  • 101 Blog Tips I learned 177 Comments
  • 30 Traffic Generation Tips 351 Comments
  • 6 Ways to Speed Up Your Site 51 Comments
  • 9 Hacks to Show Readers Your Best Stuff 21 Comments
  • 50 Simple Ways to Gain RSS Subscribers 145 Comments
  • The Best Website Taglines 127 Comments
  • What Is Success? 57 Comments
  • How to setup a 301 Redirect 52 Comments
  • The 7 Characteristics of Good Domains 76 Comments
  • 7 Ways to Promote your Site 133 Comments
  • Top 25 Celebrity Blogs 44 Comments
  • What Is A Blog? 81 Comments
  • Blog Setup: 40 Practical Tips 57 Comments
  • 10 Tips To Write Your Most Popular Post 79 Comments
  • The Blog Post Checklist 75 Comments
  • Interview: 12 Top Online Entrepreneurs 98 Comments
  • What Is Bounce Rate? 42 Comments
  • 20 SEO Terms You Should Know 28 Comments
  • How To Choose A Blog Name 15 Comments
  • 10 Tips to Sell Your Website on Flippa 37 Comments
  • Top 25 Web Design Blogs 80 Comments
As featured
CNBC copyblogger problogger Entrepreneur Lifehacker Hubspot Business Insider Wordpress Showcase
Online business done right. Take your marketing efforts to the next level with tips and resources to get visitors to your website.

All Content Copyright © 2023-2023. Daily Blog Tips unless otherwise noted or credited.

DBT is an independent website. The views expressed on this site may come from individual contributors and do not necessarily reflect the view of DBT or any other organization.

Privacy Policy| Terms of Service | About | Contact

777 Brickell Ave #500-14648, Miami, FL, 33131, US