Virtual hosts subdomains pointing to wrong directory

12,313

When you have several VirtualHosts one of them is the default VirtualHost. It's the first one in alphabetic order of the file containing the definition of the VirtualHost.

When you remove a VirtualHost. If you still have an entry in the Hosts file or a DNS record, when the query is performed on your Apache server, if it cannot find the right VirtualHost (ServerName or ServerAlias), then the default one is taken to process the answer.

When you add a new VirtualHost, if you make a mistake in the ServerName or ServerAlias you'll also have the default VH.

Update

Now that the question is complete I can see you are not using ServerAlias in the right way. All your subdomains should be listed in ServerAlias directives, without the http:// So you should have:

ServerName web1.com
ServerAlias www.web1.com
ServerAlias games.web1.com

You could maybe try a *.web1.com. Else when you use a name which is not listed the default vhost is used (and here vhost web2.com is defined before so it's the default one on this port)

Share:
12,313
www.amitpatil.me
Author by

www.amitpatil.me

php, jquery, ajax, css3, html5, Node, Express, MongoDB, Angularjs, Ionic, Cordova, Phonegap, Modx, Wordpress, VB6 blogger, Music fan

Updated on August 31, 2022

Comments

  • www.amitpatil.me
    www.amitpatil.me over 1 year

    I created 2 virtual hosts http:// web1.com:2107 pointing to "/var/www/web1" folder and second http:// web2.com:2107 pointing to "/var/www/web2". all subdomains in web2 are working fine, But in case of web1.com:2107 i am able to see only home page. when i try to open any subdomain from web1 like http:// games.web1.com:2107 it points to web2's home page ie /var/www/web2/ directory

    Is there anything wrong in configuration ?

    below is the code for hosts i am using

    <VirtualHost *:80>
    ServerName  web2.com
    ServerAdmin [email protected]
    ServerAlias http://www.web2.com
    DocumentRoot /var/www/web2
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/web2>
        Options Indexes FollowSymLinks Multiviews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride All
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
    
    ErrorLog    /var/www/web2/error80.log
    CustomLog   /var/www/web2/access80.log Combined
    </VirtualHost>
    
    <VirtualHost *:2107>
    ServerName  web2.com    
    ServerAdmin [email protected]
    ServerAlias http://www.web2.com
    DocumentRoot /var/www/web2
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/web2>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride All
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
    
        ErrorLog /var/www/web2/error.log
    CustomLog   /var/www/web2/access80.log Combined
    LogLevel warn
    
    CustomLog /var/log/apache2/web2.com_access.log combined
    
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
    
    </VirtualHost>
    

    ===================================================================

    NameVirtualHost *:2107
    <VirtualHost *:2107>
        ServerName web1.com
        ServerAlias http://web1.com
        DocumentRoot /var/www/web1
        <Directory /var/www/web1/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        </Directory>
        ErrorLog        /var/www/web1/error.log
        CustomLog       /var/www/web1/access.log Combined
    </VirtualHost>
    
    • jkulak
      jkulak about 11 years
      I had the same and in my case part with "NameVirtualHost *:80" was i a file that was not included in main httpd.conf - so vhosts were not enabled at all, and therefor all pointing into default vhost.
  • www.amitpatil.me
    www.amitpatil.me over 12 years
    Yeh, i forgot to mention it but i restarted the service 2-3 times
  • SuperTron
    SuperTron over 12 years
    Oh I see, sorry I couldnt be of more help then, glad you got it working though :P
  • www.amitpatil.me
    www.amitpatil.me over 12 years
    There are less chances og making mistake i have as much as less code for new VH, Below is the code NameVirtualHost *:2107 <VirtualHost *:2107> ServerName tab.com ServerAlias tab.com DocumentRoot /var/www/tab3 <Directory /var/www/tab3/> Options Indexes FollowSymLinks MultiViews AllowOverride All </Directory> </VirtualHost>
  • regilero
    regilero over 12 years
    AllowOverrid All means you can have .htaccess files adding things to your conf. Put None instead, and remove Multiviews
  • www.amitpatil.me
    www.amitpatil.me over 12 years
    I made the changes, but still getting same problem...when i open subdomains it points to wrong directory :( is there anything i am missing ??
  • regilero
    regilero over 12 years
    add different AccessLog directives in your VH and check the one which is really hit. Run Apache2 -S to list available VH, etc. And edit your questions with code from both VH, not just one.
  • www.amitpatil.me
    www.amitpatil.me over 12 years
    I just tried adding different access log for both the VH, when i open web1 it logs in web1's log file, web2 is logging in web2's access log. there is no mixing
  • regilero
    regilero over 12 years
    .. So maybe make a clear explanation of your problem. In apache side you have two VH. works well. Make a clear test case. complete . so that we could find something strange inside. Maybe you forgot the port in your link.
  • www.amitpatil.me
    www.amitpatil.me over 12 years
    Sorry if i have confused u :( ok let me explain again. There are 2 VH. Home page and all subdomains from www.web2.com:2107 are working fine. but when i open web1.com:2107 only this time it points to correct directory...whn i try to call any subdomain like games.web1.com it points to root directory of web1.com:2107
  • regilero
    regilero over 12 years
    @Amit Patil: you are trying to be an example of bad-way-of-asking-a-question :-) . Stop commenting. Edit your question and put inside a clear explanation of your problems. Maybe by just trying to explain yourself what is the situation you'll find an answer by your own. But if you do not do this step no one will find your problem.
  • Yohanim
    Yohanim over 4 years
    Full Strict or just only full?