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

14 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:
    April 2nd, 2022 at 19:31

    … [Trackback]

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

  3. เช่าเครื่องเสียง Says:
    May 30th, 2022 at 06:53

    … [Trackback]

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

  4. nova88 Says:
    June 4th, 2022 at 19:17

    … [Trackback]

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

  5. sbo Says:
    June 17th, 2022 at 21:49

    … [Trackback]

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

  6. nova88 Says:
    June 26th, 2022 at 23:12

    … [Trackback]

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

  7. Buy HammerDown 45 Colt Says:
    July 5th, 2022 at 18:29

    … [Trackback]

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

  8. website oficial do app Says:
    July 15th, 2022 at 17:53

    … [Trackback]

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

  9. winbet 娛樂 Says:
    July 28th, 2022 at 21:39

    … [Trackback]

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

  10. buy henry rifles Says:
    August 3rd, 2022 at 08:45

    … [Trackback]

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

  11. sbo Says:
    August 22nd, 2022 at 09:27

    … [Trackback]

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

  12. รับซื้อรถโฟล์คลิฟท์ Says:
    August 27th, 2022 at 21:58

    … [Trackback]

    […] There you can find 3592 more Information to that Topic: compdigitec.com/labs/2011/01/24/redirect-non-www-domain-to-www-in-apache/ […]

  13. Darknet Market Says:
    August 31st, 2022 at 10:54

    … [Trackback]

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

  14. maxbet Says:
    September 19th, 2022 at 14:55

    … [Trackback]

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

Comments