0

I had a very strange behavior with my Apache setup and then fixed it, however I would like to understand what really happened. My httpd.conf LOOKED like this:

<VirtualHost *:80>
  ServerName mysite.com
  ServerAlias www.mysite.com
  ...
  <Directory "/path/to/mysite.com">
  </Directory>
</VirtualHost>

=== THEN, I wanted to add a 2nd different host, with the same IP but different domain name, so I added the following after to the file (after the above lines)

<VirtualHost adifferentdomain.com:80>
  ServerName adifferentdomain.com
  ServerAlias www.adifferentdomain.com
  ...
  <Directory "/path/to/adifferentdomain.com">
  </Directory> 
</VirtualHost>

====== THE end result: When you browsed for mysite.com you would see the content from adifferentdomain.com . When you browsed for adifferentdomain.com you would see adifferentdomain.com

So what exactly happened? Was that a standard redirection? Or what exactly was the "understanding" from a regular browser?

My question is because this affected my google ranking dramatically, and I want to know what exactly did Google see.

1
  • I want to point out, that I have already corrected the problem, my question is not how to properly do this, my question is an explanation about what was the outcome of my error from a regular browser, google, or dns/ip/redirection perspective.
    – smorhaim
    Oct 19, 2010 at 21:45

1 Answer 1

2

This happened because when Apache started up and read this file, it resolved the IP for adifferentdomain.com and, since that block came before the * virtual host, all requests coming in on that IP were then serviced by that virtual host configuration. No redirection, as such, actually took place. Apache determined that virtual host block to be the appropriate one to service any request coming in on that IP.

Accepting hostnames in the VirtualHost directive is merely a convenience for the case where the IP may change/you want your configuration to look more readable when you have a number of domains/hosts with different IPs served by one Apache instance.

As you no doubt found out, the ServerName and ServerAlias components provide the appropriate magic for this (but only to clients that perform HTTP/1.1 requests and provide the hostname they're trying to request from in the request headers).


Update

In response to your comment: if you wanted to replicate this problem in the way you describe (2 hosts use name based virtual hosting correctly, where one does not) you would need to properly define the vhosts (with a corresponding NameVirtualHost directive) for domain1 and domain3, with the domain3 vhost directive coming first. Assuming all the domains served up by this apache instance are served off the same IP:

# Let Apache know know you want to do name based
# virtual host matching on this IP/port pair
NameVirtualHost *:80

<VirtualHost *:80>
  ServerName domain3.com
  ServerAlias www.domain3.com
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName domain1.com
  ServerAlias www.domain1.com
  ...
</VirtualHost>

It's important that the vhost you want to be the default come first and that the vhosts that share an IP you wish to perform name based virtual host matching on have a NameVirtualHost line and use the same IP/port combination. If you don't use the NameVirtualHost line, Apache will default to the first found best match (specified IP beats * as you found out before).

2
  • Thank you for the answer. A second question arises... If I want to replicate the problem with a 3rd domain in this manner: domain1.com shows domain1.com domain2.com shows domain3.com <-- domain3.com shows domain3.com Keep in mind in the original problem, the address bar show domain1.com while displaying domain2.com content. Thank you.
    – smorhaim
    Oct 22, 2010 at 4:17
  • I've updated my answer to address this.
    – mark
    Oct 25, 2010 at 1:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .