Apache Domain goes to wrong Domain when not entering WWW

To totally unlock this section you need to Log-in


If you run Apache on your server and you also want to run multiple sites then we use something called Virtual Hosts. If you run multiple domains then you may come across the following issue. Let's say you have two domains on your server: Domain A and Domain B. You can contact Domain A in your web browser with the following:


You can contact Domain B with the following:


However when you enter domainB.com into your web browser you are sent to DomainA.com

This is an issue with your virtual hosts file within Apache. The exact problem is you have not specified the servername property and the serveralias property. if you check your virtual hosts file it will look like the following. You can either use webmin to manage your virtual hosts files or do it manually.

DocumentRoot "/var/www/domainb.com/"

Options FollowSymlinks
RewriteEngine On
allow from all
Options +Indexes

We need to ad the following lines to your virtual hosts file in apache. Servername and Serveralias. Below is a good Apache Virtual Host example:

DocumentRoot "/var/www/domainb.com/public_html"
ServerName domainb.com
ServerAlias www.domainb.com

Options FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://domainb.com$ [NC]
RewriteRule ^(.*)$http://www.domainb.com/$1 [R=301,L]
allow from all
Options +Indexes

You will also notice the RewriteCond commands, this means that ip a user enters the standard http://domainb.com then the URL is rewrote to www.domainb.com, this is an SEO benefit as search engines only see one domain.

This means that when a user attaches to both www and http then they are directed to the correct site.