Maybe it’s just me, but I get kind of annoyed when websites are accessible by both their non-www and www URL. If you attempt to go to my site by using http://trevorfitzgerald.com/, you’ll see that you’re automatically sent to http://www.trevorfitzgerald.com/ instead.
Why should you do this?
If you require people to login to your site, you probably give them a cookie so they don’t have to login each time. But that cookie is only good for one or the other URL, not both. So if somebody logs in using http://yoursite.com/ and comes back later using http://www.yoursite.com/, they’ll be forced to login again, even though a cookie already exists for them using the other version.
Also, forcing one URL variety over the other ensures that you won’t be double indexed or receive split page ranks in the search engines. While Google does a pretty good job on their end of preventing this, it’s possible that you could have two distinct page rank values for each version of the web page, even though they are 100% identical in content, but only differing in the presence of “www” in the URL.
How do you do it?
It’s simple really. Just add the following code to your .htaccess file in the highest folder of your web directory and yor visitors will be forced to use the www version.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trevorfitzgerald.com [NC]
RewriteRule ^(.*)$ http://www.trevorfitzgerald.com/$1 [L,R=301]
Excellent suggestion. The only problem is, it won’t work for sub-domains….. i.e. MINE…. LoL
thanks!
Just Googled for this and came to this site. Thanks for the info.
Subscribed to feed, stumbled
Wow, I was looking for this code and google redirected
me to this great site.
Thanks for the .htaccess code.
Mohsin
Thanks for this! I reversed it though, to force a non-www dedirect.
You can do the same for subdomains, assuming you have the ability to edit and apply the .htaccess file. Try:
RewriteRule ^.*foo.bar.com(.*)$ http://www.foo.bar.com$1
That may lose a forward slash in there, but tinker with it, and it will work
that helped a lot without having to restart the whole apache. thanks.
I use Google Webmaster tools (sItemaps>>Preferred Domain)for this purpose…
and my SE juice PR, backlinks remain remain for both domains ;
Great fix.
What do I do with url like
http://bar.com/foo?
it wom’t work.
Thanks for help in advance.
Great resource!
I found your site on Freekat’s Knowledgebase…
All of this talk about google is making me nauseous.
Thanks!
Just what I needed.
Thanks for the great, easy to follow advice. It worked like a charm!
The WWW. really is obsolete, I remove it completely…
RewriteCond %{HTTP_HOST} ^trevorfitzgerald.com [NC]
RewriteRule ^(.*)$ http://trevorfitzgerald.com/$1 [L,R=301]
Check out http://no-www.org/
Daz, it’s not that obsolete, if someone types in the domain name minus .tld into their browser and hits CTRL+Enter their browser auto-adds www.{domain}.com.
Yeah, I reversed this, I hate www. I never type it any more.
Any thoughts on using https? When I use the above code it changes my https as well, so…
https://mysite.com becomes http://www.mysite.com
Making my ssl unusable
Is there an adjustment in the code to take this into consideration. Want the rest of the site to be http://www.mysite.com
Thanks
Jake
never mind i had two of these….i put it in the wrong file. thank you very much on the instructions
Thanks for help
Great thanks for the tip. I used it on http://www.simplyexcited.co.uk
Thanks, didn’t even realize this was a problem until a user let me know. Googled it, found this site, fixed the issue in about 80 seconds. You are the best. I used it on http://www.talkairline.com
Most hosting sites redirect everyting to your domain that is not an explicit subdomain. The use a wildcard like in our case: *.internani.com to send not only internani.com and http://www.internani.com but everything to our site. So to force everyone to go to http://www.internani.com we use a ! (=NOT) in the RewriteCond like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.internani.com [NC]
RewriteRule ^(.*)$ http://www.internani.com/$1 [L,R=301]
Could someone explain the [NC] and [L,R=301] parts of the code for those of us who would like to learn what is going on in the back end of this code?
I assume the 301 references redirects, but now sure what the L,R and NC are doing. Thanks.
[NC] means No Case, or Not Case sensitive, so the value of HTTP_HOST will be compared with the string on the right side case insensitively. L stands for Last, if the RewriteRule applies it will be the last rule that gets executed, after which the proces leaves the .htaccess. R simply stands for Redirect. R=301 means Redirect and send a 301 Status code. 301 means that the Redirect is permanent.
Nani,
Thanks for the explanation and your previous tip!
Thanks im using this on my site
Great thanks for the tip.
Thank you Trevor. My tip contained an error though, it should be $1, not just 1:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.internani.com [NC]
RewriteRule ^(.*)$ http://www.internani.com/$1 [L,R]
If you want to prevent hotlinking (people ’stealing’ your bandwidth by directly linking to your pictures and flash movies) you could add something like this after adding the redirect code.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^.www.internani.com/.*$ [NC]
RewriteRule ^(.+\.(gif|jpg|jpeg|bmp|png|swf))$ – [L,F]
Sorry Trevor, that was wrong, please change that anti-hotlinking code into this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^.*.internani.com/.*$ [NC]
RewriteRule ^(.+\.(gif|jpg|jpeg|bmp|png|swf))$ – [L,F]
Nani,
Beautiful! I actually need that for a current project of mine. Perfect timing!
Thanks
nice one.. worked like a charm
thx a lot great little workout!
Thanks Trevor, tried a couple of these and your seems to work a charm first time. I was having an issue with my SSL certificate appearing invalid at http://mantality.co.za and working fine at http://www.mantality.co.za – so this has sorted that out perfectly.
My boss registered several domain names to keep our competition from getting them. What is the best thing to do for these new domain names to prepare them for proper SEO use? I have heard that the older the domain is the more impact they have on SEO. Do I need to point them to one of our existing web sites while they age, or do some sort of 301 redirect thing? What would be the best “white hat’ process to do with these domains to help us out when we start to use them for web sites?
Exactly what I was looking for, thanks!
found this post really helpful…thanks a lot..
Searched it in google.
Actually, you may want to use this code just for looks or for redirecting when an SSL certificate is just for the www domain name. Having said that, I’d like to say that your initial statement about leaving a cookie is incorrect.
You can easily set cross subdomain cookies simply by leaving out the explicit subdomain alias.
setcookie(‘.domain.com’) <— this will work on www, abc, def, nothing, or anything
To those who think that no-www is better than with-www: http://www.web1marketing.com/blog/index.php/archives/whats-wrong-with-no-www/
Thanks!! Used on http://lakesidecontrols.ca
Thanks. It is really helpful.
Great! lol-email.com is now http://www.lol-email.com
Got that done in 2 mins thanks to you!
Why don’t you have it go the other way instead, redirect http://www.domain.com to domain.com? This way it’s shorter… just wondering if there’s any reason or if it’s just a matter of preference.
And if it really does matter, since like you said google is on top of things.
Great Help.Thanks dude
Thanks for the help. it’s good for all developer who is looking for domain redirection.
thanks brother, I work in web development, and it is a bit embarrassing, but I have gone years without this help. You are a true knight.
Nice one, i find the following works better
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
When I have an option (not possible on all hosts), I always redirect to the non-www version. Think of it from a branding and public relations point of view. You want people to see your domain first, not the meaningless “www.”
You do want to help people to remember your site and name, don’t you?
Works like a charm^^
thx mate
Thanks, worked perfectly!