Search Engine Optimization

How to do a 301 Redirect Properly on Apache or Windows Servers

By May 7, 2010 January 4th, 2013 63 Comments

In today’s blog post we will discuss how to implement a proper 301 redirect on Apache or Windows servers from one domain to another. Redirects are technical and we see a lot of sites where 301 redirects are not implemented properly. You might want to do 301 redirects because of a number of reasons: redirecting the non www to www and vice versa, or if you are changing your domain or a file within the same domain.  This is also a great post on ways you can fix your 404 error pages.

Before we enter the technical details, it is important to understand the importance of a 301 redirect from non www to www version of your site (or vice versa). First having two versions of your site can create duplicate content, which may result in your website being penalized by search engines. Secondly and most importantly, when you acquire links it’s always much better to have them pointing at one version of the site versus distributing it among two pages which dilutes the search engine importance to your domain.

301 redirects is the most preferred way of handling duplicate content. Other ways include using the ” rel = canonical” tag (don’t use for cross domain, Yahoo/ Bing still don’t recognize it), blocking files in robots.txt and the meta noindex tag.

Let’s dive into the technical details:

How to do a 301 redirect for an Apache server:

Step 1: To implement a 301 redirect the file we need to work with is the .htaccess file. To access the file you need to go into your FTP and look into the document root.

Step 2: If you can’t see it, enable viewing of hidden files since the .htacess file is hidden. If there is still no .htaccess file present , create one with a simple text editor.

Step 3: Insert this code in the file:

Code example from non www to www:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC] RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

Obviously you will need to replace ‘example’ with your own domain name.

Also make sure the Rewrite Engine is turned on, you will just need to turn it on once.

Step 4: Save and Test it!

How to do a 301 redirect for a Windows server:

When setting up a site in IIS, the normal process is to create one account for the site and add both www and non-www versions of the domain name to the host headers for the account. This creates a canonicalization issue;  and the site will then be available at both www and non-www URLs.

Step 1: Get access to the Windows Server navigation Panel. Navigate your way to the Internet Services Manager (Programs — Administrative Tools — Internet Services Manager).

Step 2: Create 2 accounts for the site within IIS: one with the www version of the domain in the host header and one with the non-www version of the domain. All of the site files can be placed in the preferred version and a single page in the other.

Step 3: Right click on the single page you want to redirect FROM and choose Properties. The Properties box will now appear.

Step 4: Change the redirect option to “A redirection to a URL” and type in the new URL in the box provided.

Step 5: Be sure to check the box marked “A permanent redirection for this resource”. If you leave this box unchecked, you will create a 302 (temporary) redirect, which is not permanent or beneficial from an SEO standpoint in this situation.

Step 6: Test it!

Update July 6, 2011 – Doing a www redirect for Front Page

As I see some of the comments below pertaining to Front Page, it was a matter of time before I had to do one for this God-forsaken MS product myself.  Here’s how I did after some trial and error:

1.  First, you have to identify weather you are running Linux or Windows.  This works for Linux.  Apparently, there is an extension called FollowSymlinks which needs to be turned on, as well as Mod Rewrites, so call your host provider for that one.

2.  FP uses several .htaccess files – one in the main directory structure, and 3 other .htaccess files called “super files”.  You will find these other .htaccess files here:

/_vt_bin/.htaccess

/_vt_bin/_vti_aut/.htaccess

/_vt_bin/_vti_adm/.htaccess

3.  Make sure this is at the top of all 4 .htaccess files: “Options +FollowSymlinks” underneath “# -FrontPage-”

4.  Underneath this, add your 301 redirect command:

RewriteEngine On RewriteCond %{HTTP_HOST} ^yoursite.com$ [NC] RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]

Here, I did a 301 from non-www to the www, because for SEO purposes, most people have more inbound links pointing to the www version.

That’s is – this should work!

 

Ryan Kelly

Ryan Kelly

Ryan is the founder and CEO of Pear Analytics and has helped hundreds of customers with their Internet marketing since 2003. He has spoken on various topics of SEO, Analytics and other marketing at conferences in New York, Chicago and Vancouver. Clients he has consulted include Sears, KMart, CareerBuilder and PEER 1 Hosting. Ryan currently teaches two Internet marketing classes at Trinity University in San Antonio.

63 Comments

  • skywalker says:

    Thank you.I am trying this tutorial

  • Ryan Kelly says:

    @skywalker, be sure to check this post again this week. We found some edits/modifications we need to make to it:). Hope it helps you!

  • Manish Patel says:

    Thank i was looking for this kind of solution for windows in google search i am always getting solution for apache and but i was looking for windows server thanks a lot

  • Thank you for this interesting post.

  • Henna says:

    hi,
    when I checked my site page analysis in pearanalytics its says duplicate content detected and my website has two versions. I read this post but can I do the redirect in my .htaccess but I am using WordPress blog. I dont want to mess it up..Please Help…

  • Mauricio says:

    This is great! I used this along with another example to get the version for using non-www.

    I have a question, though. When I updated the htaccess file and went to http://www.blackberryrocks.com it did redirect to blackberryrocks.com but I got a cached page (I believe).

    I use W3 Total Cache. Is there a way to prevent this? Feel free to email me if need be.

    Thanks!

  • Ryan Kelly says:

    Hi Mauricio,

    yes, WP Cache has been problematic for me too in some cases when I update a page, and then can’t see the change. I just ask my CTO to fix it, and he does something on the server, but it’s not just a simple clear through WP. I’ll ask him and post the answer here later.

    Thanks!

  • Andy says:

    Hey,
    I would like to say thanks for pointing out the duplicate content issue..
    Using ftp I was able config the Apache server to direct the dupe non-www site no problem..
    Again big thanks..
    However no when I try access the site via frontpage it denies me.. via
    http://www.absoluteclassdiscos.com
    Can get in via non http://www.. but can’t save.. oh well
    I need to change “RewriteEngine On” to Off
    then I can edit when need be… must be something simple I am over looking..
    yeah, yeah, I know frontpage is junk..shh.. has it’s uses for me.. 😀
    Anyone have any ideas.. ??
    Cheers
    Andy

  • Ryan Kelly says:

    Hi Henna,

    there is still an .htaccess file even though you have WP installed. Go to the root, and make sure you have “show hidden files” checked, because you can miss it. You can put the redirect code in there and save it. You can also use the rel=canonical tag on your home page, and there is a plugin from Yoast for that. Hope this helps!

  • Andy says:

    In a follow up to my problem with FrontPage no longer working after applying a 301 direct
    (The solution)
    In the following 3 folders
    /httpdocs/_vti_bin
    /httpdocs/_vti_bin/_vti_adm
    /httpdocs/_vti_bin/_vti_aut
    each will have a .htaccess file starting with the following 3 lines of text..
    # -FrontPage-

    Options none

    simply change Options none to Options +FollowSymlinks
    with your ftp program and your done.
    Hope this helps someone 🙂

  • Ryan Kelly says:

    Hi Andy,

    thanks for sharing that solution!

  • De Razor says:

    Hi Ryan,
    In step 3 can you explain what you mean by replace “example”.
    Do I just swap the text ‘pearanalytics’ for my .com domain name only or is there something else I’m missing?
    Thanks

  • Ryan Kelly says:

    Oh, whoops! Yes – we mistakenly used pearanalytics.com instead of “example.com” as the copy indicated. Changing that now…..good catch!

  • dBune says:

    This was a helpful post. Did implement this in the .htacces file and it seems to be working all fine. However, had to implement this at 2 places a main .htaccess file in the root directory and also in the .htaccess file in Joomla sub directory installation.

    I was under the impression, redirection in the .htaccess in the root dir should work for all sub dir/domains too… But anyways, things work.. so thanks for this tutorial.

  • Ryan Kelly says:

    Thanks for the comment! You can also configure your redirect straight in Apache too, and then you wouldn’t have the problem of conflicting .htaccess files.

  • Robin says:

    You saved me 🙂 Thanks and Cheers.

  • MHEL says:

    i’m still confused… can i do this even if i’m not hosting my website?

    the info is very helpful, i just don’t know where to start

    🙁

  • Romy Misra says:

    Yes, you can do this if you don’t host your site. But you’ll need access to the .htacceess file in your FTP (assuming it’s Apache). Let me know what questions you have (support@pearanalytics.com) and I’ll rewind a bit more to how you can get started!

  • MHEL says:

    i’m a real newbie…sorry… i’m using windows XP as of the moment, how will I know if its Apache?

    🙁

    i keep on checking my site through your analytics and it’s been a big help for me…

    but i’m a real ignorant come to the 301 info , my site still comes up as a duplicate content..

    🙁

    thank you much ^_^

  • Ryan Kelly says:

    Hi Mhel,

    Apache is HTTP server software, and works on a variety of operating systems. The 301 is a bit complex for beginners, so the best thing to do is put a rel=canonical tag in the header of the page, just like you would a title tag. Here is an example:

    link rel=”canonical” href=”http://www.site.com/correct-location/”

    You would put either “www” or leave it out, depending on what you prefer (I would pick the version that has the most links pointing to it).

  • Kare4n Cole says:

    I am listed on Google for this website as having duplicate content due to its showing on both http;//rainbowriting.com and http://www.rainbowriting.com but I used this mehod in this article: Implementing 301 redirects for a Windows server – however, once I logged into Internet Information Services on my computer I didn’t know how to “Create 2 accounts for the site within IIS: one with the www version of the domain in the host header and one with the non-www version of the domain.” I have no idea how to properly do this. I found a place that would let me add websites, but it asked for a lot of other info that I simply don’t have. Can anyone help step me properly through this? Thank you in advance for a prompt and useful response.

  • Karen Cole says:

    I tried using the method outlined under Implementing 301 redirects for a Windows server in this tutorial, but after opening the Internet Services Manager panel I was unable to “Create 2 accounts for the site within IIS: one with the www version of the domain in the host header and one with the non-www version of the domain.” Can someone please tell me exactly how to go about doing this? I was unable to fill in the info, such as site name, physical path, IP address and host name. Please help me!

  • can some one give me a 404 error page template

  • GT says:

    Really useful post, but I wonder if I’m missing something? I’m working on improving the duplicate content situation on several Joomla sites. I’ve added the non to www code to the htaccess file, and I also tried the code given by Andy above for redirecting index.php to the main URL. (Bit of an issue for Joomla really, you could end up with FOUR versions of your homepage! non, www, non index.php, www index.php)

    However, none of it seems to be working. It’s there in the htaccess file ok, but no matter what browser I use there seems to be no rewriting going on whatsoever. The Joomla SEF url rewriting which is also in the htaccess file is working fine, but not these changes. This is the case on several sites. Am I missing something? Ryan Kelly said above in reply to a post regarding Joomla: “You can also configure your redirect straight in Apache too, and then you wouldn’t have the problem of conflicting .htaccess files.” What does it mean exactly to configure my redirect straight in Apache?

    All suggestions greatly appreciated!

  • GT says:

    Hold the bus! I’ve solved the problem referred to in my previous comment. It seems that adding the code to the end of my .htaccess file is not the same as adding it just after the RewriteEngine On command. I moved the code (both the one from the post and Andy’s clever code above) and now they both work like a dream!

    Thank you Pear and thank you Andy for very helpful bits of code, and perhaps it might help to add that specific point to the original post – add the code after RewriteEngine On to ensure it works!

  • Ryan Kelly says:

    That’s great news! I’m glad you got it worked out.

  • Ryan Kelly says:

    I saw a blog post on Smashing Magazine once of the coolest 404 pages. Maybe you can get some inspiration over there!

  • Andy says:

    GT my post ended up very scrambled and parts of it are even missing..
    and … well since posting that my ideas and experience have expanded..

    RewriteCond %{THE_REQUEST} /index.php? [NC]
    RewriteRule ^(.*/)?index.php?$ /$1 [R=301,L]

    actually creates a double redirect..
    the non index.php first will redirect to the non www root .. THEN.. to the www root
    so..
    use this instead.

    RewriteCond %{THE_REQUEST} /index.php? [NC]
    RewriteRule .? http://www.your domain.com [R=301,L]

    Will create a single path redirect for any index.php request 🙂
    The same rule above can be adapted if you have moved any pages.

    http://web-sniffer.net handy site to see and test the server responses for different urls

  • Robert says:

    Thanks for posting this. I know it’s a simple thing but every other post I found showed methods that didn’t work properly for all cases (like in a subdirectory with phpbb). I wish I found this sooner.

    -Robert

  • Akilah Kaui says:

    Thank you very much for the information great post, found it on Yahoo. Thanks.
    Regards
    Al Khafji

  • Fantastic blog! I really love how it?s easy on my eyes and also the information are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which ought to do the trick! Have a nice day!

  • Ryan Kelly says:

    glad we could help, Robert!

  • Ryan Kelly says:

    Thanks Karine! Yes, the RSS “should” work!

  • jenny says:

    thanx for the great tips

  • […] Options See how to fix yourself · More info on 404 errors · […]

  • Joem says:

    Thanks! This worked like a charm

  • […] 301 redirects are fairly simple to implement. I’m not going to go into the specifics of how to do it, but if you search for “301 redirect”, you’ll find tons of articles with step-by-step instructions. This is a good article detailing how to go about setting up a redirect: http://www.pearanalytics.com/blog/2010/implementing-a-proper-301-redirect-on-apache-or-windows-serv… […]

  • Web Applications Developer says:

    Hi,

    Thanks for providing this easy step by step guide. It worked …

    Regards,
    Web Applications Develoeper
    http://www.web-applications-developer.com/

  • amanda says:

    I have a problem – I am using the XML Sitemap generator plugin on my wp and when I did your free analysis it said it couldnt be found and says my robots is blocking google still not sure why but I know someone hacked and DDOS attacked my site. Well now I got this error in google webmaster tools that says my sitemap had a URL issue so I read online to change the sitename URL in wp dashboard to remove or add the http://www., in my case I had a www. so I removed it. Is this going to create problems, if so what Can I add in htaccess? Thanks 

  • […] content, and a small chance that both will not rank at all. Avoid duplicate content! Your Options See how to fix yours I found that my forum has a few entries in google for .org and .net this shouldnt' happen, how do I […]

  • Thank You Thank You Thank You!!!!! xxxxxxxx lol

  • Brenda says:

    Its quite helpful for me as on my website i tried this and its great

  • Anonymous says:

    como intalar o sitemap em meu blog?

  • Ben Holland says:

    This is fantastic!! Helped me out so much. Thanks

  • Anonymous says:

    Been searching for this everywhere! Really helped me out. Thanks

  • Hi. I came across this post and I think you might help me. I did some SEO analysis on my blog and it said “We’re detecting that you’re not redirecting the non-www version of your website to the www version (or vice versa). By not doing so, you’re creating essentially two versions of your website that can confuse the search engines when it comes time to rank your site.”

    i am using blogger as a host so not sure how i can change that. I thought i had that done…. i selected the redirection.. can you help me»?

  • SalehmuhammadAria says:

    Hi, there is a question my site is hosted at http://hostgator.com i wanna move it to the cheapest hosting http://www.varyhost.com. will be there any effect on my site’s SEO?

  • SalehmuhammadAria says:

    Hi, there is a question my site is hosted at http://hostgator.com i wanna move it to the cheapest hosting http://www.varyhost.com. will be there any effect on my site’s SEO?

  • doubtingthomas72 says:

    hi,

    how do i turn rewrite engine on?

  • CodyThomas1 says:

    I’m not too technical but I could handle this for my site – http://www.1capecoral.com – and hopefully it will do good to my site. Thank you for the tip!

  • paaahcom says:

    Hi there,

    I came to this post over the SEO analysis but I am no sure what I need to do.

    My website is wordpress based and it redirects the www into http:// already. Could you please check that for me please? I appreciate much 🙂 paaah.com

  • MideSteve says:

    how do I do this using blogger.I have a blogger blog and wants to fix this problem.

  • Ryan Kelly says:

    @MideSteve I’m not sure that is possible on a hosted blog like that. You might try going into Google Webmaster Tools and setting your “www” version there.

  • cobyheise says:

    Sorry if I missed this topic being covered elsewhere in the comments (I didn’t look). I have a conundrum: About 5-7 years ago, I implemented this 301 redirect procedure to point all www traffic to the non-www version of my website (ie. entering “www.mysite.com” will take you to “mysite.com”). I chose to do it this way b/c it was around the time when many popular websites were just starting to “drop” the “www’ from their domain names in their marketing materials and anywhere else their domain was advertised. It also seemed, at the time, that public consciousness was leading many folks to finally realize they didn’t need to enter “www” into their browsers’ address bar to properly reach a given website (which maybe only became possible w/ a certain iteration of one or more popular web browsers; I can’t remember exactly), and you could say that I “jumped on the bandwagon” and made my website behave in that exact regard. However, in reading your last sentence in the article above, as well as many other articles that say the same thing, I’m realizing now that it may have been a better idea to direct non-www traffic to the www version of my site, merely b/c that’s how it’s typically done, on average. My question is, should I now change my 301 to do just that, or am I better off just leaving it alone and accept that my website will never display “www” in the address bar, whether I like it or not? I don’t know if the potentially negative impact it could have on my SE rankings (how negative would it be, if at all?) is enough to dictate that I not change what I already did years ago. Any insight on this is greatly appreciated!

  • Ryan Kelly says:

    Coby,
    if you’ve already made the non-www version your “canonical” URL, that’s fine – no need to change it back. You can also go into Google Webmaster Tools and select which style you prefer, in addition to adding the “rel=canonical” link attribute to your meta info in the header. Hope this helps…

  • DannyLeary says:

    I’ve implemented a redirect from www to non-www. I tried with two different methods. Both seemed to redirect all www urls to the non version just fine when i manually tested it in my browser. One of the methods i used was to manually add the rewrite code to my htaccess file in keeping with the instructions on your how-to. Despite seeming to work, your analytics tool said it wasn’t redirecting properly. The other method was using a Joomla canonicalisation plugin. It also worked great. When i had my site anaylyzed with your free tool, it removed the www redirect error and said that it was good to go. However, when i tested it again, it reverted to saying there was an error again. This despite the fact it seemed to still be working when i manually type in my www version of the url. Is the redirect in fact working properly and the analytics program just has a bug??? thanks.
     
    the url in question is http://shoutoutadvertising.com

  • Purashido says:

    Hi. I have a site in Google sites with this error, I need to fix it. How can I fix this error for a site build on Google sites? Thank you.

  • DoraEmon1 says:

    vesry nice thank you. http://www.bestpaidptc.com

  • StephaneBurayStephane says:

    Merci pour ce partage 

    http://www.assistanceinformatique76.fr

  • stefinformatique76 says:

    Merci pour ce superbe article
    http://www.princesseesmeralda.fr

Leave a Reply