• Home
  • Linux
  • Apache Domain Goes To Wrong Domain When Not Entering WWW

Apache Domain Goes To Wrong Domain When Not Entering WWW

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. Lets say you have two domains on your server/VPS. Domain A and Domain B. You can contact domain A in your web browser with the following.

DomainA.com
www.DomainA.com

You can contact Domain B with the following.

www.domainB.com

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 like I do to manage your virtual hosts files or do it manually.

DocumentRoot "/var/www/domainb.com/"
<Directory "/var/www/domainb.com/">
Options FollowSymlinks
RewriteEngine On
allow from all
Options +Indexes
</Directory>

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
<Directory "/var/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.

Tags: apache, virtual hosts, webmin

Allen White

Allen is a Consultant for ITPS in the North East of England and holds the following accreditations. MCSA, MCSE, MCTS, MCITP, CCA, CCSP, VCP 4,5 and HP ASE, AIS - Network Infrastructure.

Leave a comment

Categories

Vote!

What Web Browser Do You Use?

View Results

Loading ... Loading ...

Vote!

What do you prefer..VMware or Hyper-V?

View Results

Loading ... Loading ...