301 redirect to WWW on a 2 year old website with good SERPs and organic traffic

Hi guys,

Recently someone pointed out that my website can be accessed in both ways i.e. by typing www.example.com or example.com. He further added that Google might identify this as duplicate content and penalize my website. So now I’m thinking about 301 redirection from non WWW to WWW using htaccess method. But my website is 2 year old now and I’m getting some decent traffic from Google. Will this redirection have an adverse effect on my rankings? Is there any other way to resolve this issue? I don’t want to lose my current rankings or organic traffic.

P.S. Currently Google index my website pages with WWW.

Gogle will not “penalise” you for this, but if both versions of the page are being indexed (by other search engines as well as Google), then they are effectively competing against each other for ranking. Any adverse effect is likely to be minimal, but it’s good practice to choose one version and stick with it.

You can log into Google Webmaster Tools and set your preferred version of the site - with or without www. (You first need to verify both the www. and non-www. versions, so make sure you do this before you set up a 301 redirect. )

1 Like

Thank you for the quick reply. Am I going to see any negative effects on my search rankings after this 301 redirect? My site is also accessible from example.com/index.html Do I also need to include this URL in htacess file while writing the rules for 301 redirect?

There’s no reason why you would. If anything, there should be a slight improvement when the two versions of your page stop competing, but it will probably be so small you won’t notice.

Sorry - not sure I understand the question.

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

will redirect all non-www. pages to the equivalent www. address. Does that help?

I was trying to say that apart from WWW and non WWW address, home page of my website is also accessible via these urls:

www . example.com/index.html

Is the 301 redirect required for the above urls too?

One last question:

I also have sub-domains like fr.example.com, de.example.com So do I also need to write septate 301 redirect rules/codes in their htaccess file?

I’m trying to set up redirects so that example.com goes to www.example.com and that www. example.com/index.html, www.example.com/index.html goes to www.example.com

Tried this in the .htaccess file:

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

And it seems to be working. Is this code correct?

I’ll move your topic to Server Config, as your questions have moved on to the mechanics of the rewrite. I’ve never dealt with a site with sub-domains, so hopefully somebody more knowledgeable will be able to assist you there.

1 Like

Thank you TechnoBear for the help.

Kudos to TB for his excellent responses.

Your code is fine but I feel that I need to make two comments:

First, I would simply use the {REQUEST_URI} variable rather than creating the $1 variable, i.e.,RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
Second, I don’t advise foring Apache to rely on the DirectoryIndex (for index.html) largely because some Apache installations could require that the served file be identified (lacking mod_rewrite direction not to do so). Internally, do you redirect to \ or index.html.

On the other hand, if it works, don’t fix it!



1 Like

Thank you for the reply. Well I just tried this RewriteRule ^index.html$ http://www.example.com/ [R=301,L] in htaccess and it’s redirecting both www.example.com/index.html and example.com/index.html to www.example.com So can I keep this code or this can cause problems like indirect loop?

Can you also help me to redirect my sub-domains like de.example.com to www.de.example.com and that www.de.example.com/index.html, www.de.example.com/index.html to www.de.example.com ? Can I use the above code to redirect my sub-domains too?


First, if it’s working for you, you’re set (“don’t fix it if it ain’t broke”).

Second, yes, you can do the same thing with subdomains but take care whether you want to stay in the subdomain or back to www.example.com, i.e., if you want to retain the subdomain but force www, treat it the same as example.com, i.e., sub.example.com => www.sub.example.com (don’t delete the start anchor in the original.^example{backslash}.com regex).



Thanks DK for all the help. Do I need to include redirect code for sub-domains in the main htaccess (in the root folder) or htaccess file contained in sub-domains folder.

Hi Nick,

Because subdomain requests normally bypass the main domain’s directory, you’ve got to include your code in the subdomain’s DocumentRoot (being careful not to create a loop).



This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.