Controlling www or no www in .htaccess
When you go to punch in a URL in your browsers address bar, you may add the www. or be lazy and just put in thedomain.com, sometimes you can use both and sometimes one of the two redirects to the other version of the address. For example if you type in james-blogs.com in your URL address bar the www. suddenly is added on, why? This is because for SEO purposes it is important that you have one constant varitation while redirecting the both to chosen variation in my case I make the www. appear as I think it looks better with blog and domains with a dash in them. Read more to find out why this is important
If you were to leave and have your domain work off both variations then you could up with search engines duplicating your website content thinking it is from two different sites and by not controlling the www. or no www. could also mean that PageRank will suffer badly as your content is getting ranked differently as it thinks there are two domains. Generally search engines to a good job spotting this problem, but you can never trust the search engine to be 100%. Also a good reason to do this for website that reguire you to login, if someone logged in under the www. domain and the cookie was stored, but then went to login under the no www domain a few hours later they may have to login again because the cookie was stored with the www.
Fantastic so how can I do it?
Depending on what method you want to use there is a slightly different code so decide which one you want to use and find it below:
First of all mark sure this is at the top or near the top of your .htaccess:
RewriteEngine On RewriteBase /
Once that is present all of the following Rewrite rules will be able to work.
Force the www.
# Automatically Force the www. RewriteCond %{HTTP_HOST} ^james-blogs.com [NC] RewriteRule ^(.*)$ http://blog.jmwhite.co.uk/$1 [L,R=301]
No www. I hate it!
# Automatically Remove the www. RewriteCond %{HTTP_HOST} !^james-blogs.com$ [NC] RewriteRule ^(.*)$ http://james-blogs.com/$1 [L,R=301]
For when you place this code into your .htaccess file you would obviously change the domain name.
No www for Subdomains
Even though I go against alot of people and use www. I hate it when someone has a subdomain with www. infront it. It just looks weird I mean, subdomain.website.com compared to www.subdomain.website.com looks alot better even if your main domain use www. Don’t worry though there is also a .htaccess fix for this as well:
# Remove www. from subdomains RewriteCond %{HTTP_HOST} ^www.antitables.com$ [NC] RewriteRule ^(.*)$ http://antitables.com/$1 [R=301,L]
If you already were using the no www for your main domain you configuration may also been done and subdomains will also be redirected. Sometimes they aren’t however, so add this code in to make sure subdomains redirect to the no www.
Where is this .htaccess file?!
If you need help on finding your .htaccess file, then first off all you should look in your public_html folder for the file .htaccess if you can not see it chances are it doesn’t exist, so you can go ahead by creating a file simply .htaccess and then begin adding the above rewrite rules to it. If your website is located within a sub directory e.g. your website URL is website.com/blog then you should look for the .htaccess file within the blog sub directory, if there isn’t a .htaccess file there create one within the sub directory. If this doesn’t work then it’s probably because your web hosts doesn’t support .htaccess or the mod-rewrite command, which if thats the case you should contact your webhost and question them about it.
And thats it thats how to control your domain with one variation of either www. or no www, have fun.