Blog

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:

Implementing 301 redirects 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!

Implementing 301 redirects 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!

 

Related Posts:

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
paaahcom 5 pts

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

doubtingthomas72 5 pts

hi,

how do i turn rewrite engine on?

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

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

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

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

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

Hi,

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

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

Thanks! This worked like a charm

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!

Thanks Karine! Yes, the RSS "should" work!

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

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

glad we could help, Robert!

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

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!

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

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!

can some one give me a 404 error page template

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

Canonical tags are helpful especially if you site is suffering a current duplicate content penalty in search engines.
if using a rel=canonical tag in the header, be careful about correct usage especially the home page !
Use this as in my example

Not this !

In fact it is best to remove all references to

in the html and replace them with this

You can go a step further too.. and 301 redirect default index pages to the root in the .htaccess file.

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

above rule works for html or php, can use both safely or cut it in half, and use just the one that suit current page format.

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!

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.

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 ^_^

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

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

:(

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!

You saved me :) Thanks and Cheers.

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.

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.

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

Oh, whoops! Yes - we mistakenly used pearanalytics.com instead of "example.com" as the copy indicated. Changing that now.....good catch!

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 :)

Hi Andy,

thanks for sharing that solution!

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 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.. :D
Anyone have any ideas.. ??
Cheers
Andy

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

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!

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

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!

Thank you for this interesting post.

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.I am trying this tutorial

@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!