Domain without the 'www.' gives me the Apache 'It works!' page

Solution 1:

Traditional solution: in the httpd configuration, find:

ServerName www.domain.com

and add:

ServerAlias domain.com

However, this will give you a server that responds with the same content on domain.com and www.domain.com. It's generally considered preferable (particularly SEO-wise) to have only one canonical hostname. To do that, add a new virtual server for the redirect, instead of the alias.

<VirtualHost *:80>
    ServerName domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>

Solution 2:

This has to do with the Apache config as others have stated.

Simply added a ServerAlias to the apache config for your virtualhost will do it for you. I don't think that it is good practice to create another virtualhost have it redirect for you.

It is good SEO practice to direct everything to one or the other, I suggest using this in either you config file or in your .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Solution 3:

Check the mod_alias at your apache config file, make sure that the regex is correct for redirection.

Solution 4:

Another possible solution is that the default site is catching the traffic. This worked for me - if you don't care about disabling the default site altogether:

a2dissite 000-default
service apache2 reload