Apache Virtual Hosts Not Working As Expected

17,681

It seems the problem comes from the way you use the VirtualHost directive.

Using a fully qualified domain name for the IP address of the virtual host is not recommended. It is misleading how it works. Name based virtual hosts determine the host through the ServerName directive, and not through the FQDN in the VirtualHost directive (<VirtualHost FQDN:80>). In fact this is seen as <VirtualHost 127.0.0.1:80>

What happens is your case is documented in the VirtualHost doc, last 2 paragraphs (just before "Security"), quoted:

When a request is received, the server first maps it to the best matching based on the local IP address and port combination only. Non-wildcards have a higher precedence. If no match based on IP and port occurs at all, the "main" server configuration is used.

If multiple virtual hosts contain the best matching IP address and port, the server selects from these virtual hosts the best match based on the requested hostname. If no matching name-based virtual host is found, then the first listed virtual host that matched the IP address will be used. As a consequence, the first listed virtual host for a given IP address and port combination is the default virtual host for that IP and port combination.

So when you ask for localhost/somedir, the server will try to find from the non-wildcards VHosts declarations, but do not find any with corresponding host name (ServerName), and so it chooses as "default" the first VHost with IP:Port, and not the first with *:Port.

To solve your problem, try to use <VirtualHost *:80> in all three vhost declarations :

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/"
    ServerName localhost
    ServerAlias *.localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/laravel/public"
    ServerName laravel.dev
    ServerAlias *.laravel.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/learningLaravel/public"
    ServerName learningLaravel.dev
    ServerAlias *.learningLaravel.dev
</VirtualHost>

And reload / restart Apache.

(My only doubt about this is why Nasreddine could make a working test case with your setup.)

Share:
17,681
Eddy Freeman
Author by

Eddy Freeman

Updated on June 17, 2022

Comments

  • Eddy Freeman
    Eddy Freeman almost 2 years

    My Apache "httpd-vhosts.conf" looks like this::

    <VirtualHost *:80>
        DocumentRoot "c:/wamp/www/"
        ServerName localhost
        ServerAlias *.localhost
    </VirtualHost>
    
    <VirtualHost laravel.dev:80>
        DocumentRoot "c:/wamp/www/laravel/public"
        ServerName laravel.dev
        ServerAlias *.laravel.dev
    </VirtualHost>
    
    <VirtualHost learninglaravel.dev:80>
        DocumentRoot "c:/wamp/www/learningLaravel/public"
        ServerName learningLaravel.dev
        ServerAlias *.learningLaravel.dev
    </VirtualHost>
    

    and my "...system32/drivers/etc/hosts" also looks like this::

    127.0.0.1       localhost
    127.0.0.1       localhost
    
    // I added the following entries. The first two entries above was already there
    127.0.0.1       laravel.dev
    127.0.0.1       learninglaravel.dev
    

    When i enter "learningLaravel.dev" and "laravel.dev" into the browser, they work fine as expected. But i have other folders in my "www" folder that i use them to learn PHP and i want to be able to access the files in those folders directly from the browser like say "localhost/test/me.php". But anytime i enter such address the browser goes to the second entry in the vhosts-conf file [which prints a laravel error meaning that it can't find the file]. It seems that the first entry in the vhosts-conf file is not working and Apache bypasses it to the second entry. The first entry is suppose to be the catch all entry. I tried to swap the second and third entries to see how it will behave but it always direct the browser to the second entry instead of the catch all (first entry) that is suppose to handle addresses likes "localhost/test/me.php"

    Anytime i enter only "localhost" into the browser, it goes straight to the second entry instead of say printing the contents of the "www" folder.

    How do i solve this problem? thanks.

    • shraysalvi
      shraysalvi almost 9 years
      Just setup the same config as yours and it works as expected. (WAMP 2.5 with Apache 2.4.9 on a Windows 10 machine). What versions are you using?
    • Eddy Freeman
      Eddy Freeman almost 9 years
      @Nasreddine My WAMP is 2.4 with Apache is 2.4.4.
    • Pᴇʜ
      Pᴇʜ almost 9 years
      did you turn it off and on again? restart apache to make sure there is not an older config applied. As Nasreddine said this config is correct and worked for me a hundred times too.
  • Eddy Freeman
    Eddy Freeman almost 9 years
    Thank you sooo much "Zimmi". The problem is solved now and everything works perfect. After making changes to the Virtualhost directive, it works perfect. thanks a lot, you have saved me from hours of searching and searching.
  • sandeepkunkunuru
    sandeepkunkunuru over 2 years
    Thank you, first few lines in your answer helped address an issue that I was struggling with for last 3 hours