« Quickly generate CRUD (Create, read, update, delete) stored procedures in SSMS with SSMS Tools Pack | Home | Getting the direct link of a SkyDrive file transparently using PHP »
Redirect non-www domain to www in Apache
By admin | January 24, 2011
Search engines will often consider the non-www and www sites of a domain as separate websites (example.com vs www.example.com). In order to solve this, you will often want to redirect the non-www part of the website (example.com) to the www part (www.example.com). So this way if your visitor types example.com or www.example.com they will wind up at your home page at www.example.com.
First have your site configuration file (with the VirtualHost) opened in your favourite text editor, then make sure the ServerName of the VirtualHost is set to the www version:
<VirtualHost *:80> ServerAdmin you@example.com ServerName www.example.com <!-- other VirtualHost configs here... --> </VirtualHost>
Then add another VirtualHost at the bottom of that (in the same file), replacing your variables:
<VirtualHost *:80> ServerName example.com Redirect permanent / http://www.example.com/ </VirtualHost>
This should look something like this in the end (all in one file):
<VirtualHost *:80> ServerAdmin you@example.com ServerName www.example.com <!-- other VirtualHost configs here... --> </VirtualHost> <VirtualHost *:80> ServerName example.com Redirect permanent / http://www.example.com/ </VirtualHost>
When you are done save the file, close it and reload Apache:
sudo /etc/init.d/apache2 reload
This is a very safe and effective way to enforce your SEO and this solution does not require the rewrite engine either, which makes it very fast and safe. It performs the 301 redirect in the search-engine-approved manner and it will make sure your search engine popularity is not being cut in half by the www and non-www.
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!
Topics: Linux | 1 Comment »
May 10th, 2013 at 04:20
I found other references to Redirect permanent / http://www.example.com/ in a VirtualHost on the internet. Experimenting with this led to failure.
But this is the first article that clearly gives an example on how to do it.
With this article I managed to achieve what I wanted. Thanks.