Compdigitec Labs

« | Home | »

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 | 6 Comments »

6 Responses to “Redirect non-www domain to www in Apache”

  1. Karel Says:
    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.

  2. หวยสดออนไลน์ Says:
    June 25th, 2024 at 18:39

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2011/01/24/redirect-non-www-domain-to-www-in-apache/ […]

  3. altogel slot login Says:
    June 29th, 2024 at 05:58

    … [Trackback]

    […] Read More here to that Topic: compdigitec.com/labs/2011/01/24/redirect-non-www-domain-to-www-in-apache/ […]

  4. ยาเพิ่มขนาดชาย Says:
    July 4th, 2024 at 20:37

    … [Trackback]

    […] Find More on that Topic: compdigitec.com/labs/2011/01/24/redirect-non-www-domain-to-www-in-apache/ […]

  5. คลินิกปลูกผม Says:
    July 17th, 2024 at 00:30

    … [Trackback]

    […] Read More Info here to that Topic: compdigitec.com/labs/2011/01/24/redirect-non-www-domain-to-www-in-apache/ […]

  6. รับสั่งทำเค้ก Says:
    July 18th, 2024 at 20:18

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2011/01/24/redirect-non-www-domain-to-www-in-apache/ […]

Comments