Friday, January 28, 2011

Apache weird redirection problem

A few days ago I changed something on my Apache and it behaved strange.

For the VirtualHost, I had 1 entry

<VirtualHost *:80>
  ServerName siteA.com
  ...
  <Directory "/path/to/siteA.com">
  </Directory>
</VirtualHost>

Then added a 2nd one:

<VirtualHost siteB.com:80>
  ServerName siteB.com
  ...
  <Directory "/path/to/siteB.com">
  </Directory>
</VirtualHost>

As you see, I replaced the *:80 with the actual name. The result was that if you typed siteA.com you end up seeing siteB.com content. The address bar would still say siteA.com all the time.. but it was siteB.com content.

(My previous question as to why this happened has been answered.. this is a NEW question)

I want to reproduce this behavior but for 3 sites in the following manner:

siteA.com shows siteA.com <--- normal
siteB.com shows siteC.com <--- not normal
siteC.com shows siteC.com <--- normal

All my attempts of doing this end up making all sites point to the named domain.

How can I do this?

Thank you.

Blockquote

  • Your VirtualHost directives need to match your NameVirtualHost directive. If you have

    NameVirtualHost *:80
    

    Then they must all use

    <VirtualHost *:80>
    

    If you're mixing up name based and IP based virtual hosts, then you should use the actual IP addresses everywhere, not *. See the note here.

    From DerfK
  • Using your example and what DerfK said, you could add DocumentRoot directives to each <VirtualHost *:80> section and then just make the DocumentRoot for the "SiteB.com" site point to /path/to/siteC.com:

    <VirtualHost *:80>
      ServerName siteA.com
      DocumentRoot /path/to/siteA.com
      ...
      <Directory "/path/to/siteA.com">
      </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerName siteB.com
      DocumentRoot /path/to/siteC.com
      ...
      <Directory "/path/to/siteC.com">
      </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerName siteC.com
      DocumentRoot /path/to/siteC.com
      ...
      <Directory "/path/to/siteC.com">
      </Directory>
    </VirtualHost>
    
    From Xhantar

0 comments:

Post a Comment