Thursday, February 8, 2024

[SOLVED] Several virtual hosts on the same port with Apache 2.4

Issue

I'm trying to do something simple with virtual hosts in Apache 2.4 (using Wampserver 2.5)

I want to be able to have several virtual hosts and access them simply by :

  • www.project1.dev
  • www.project2.dev

So I made the following configuration in httpd.conf by reading the official guide :

NameVirtualHost \*:80

#
# Project 1
#

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/myProject1/"
    ServerName www.project1.dev
    ErrorLog "logs/project1-error_log "
    CustomLog "logs/project1-access_log" common
    <Directory "C:/wamp/www/myProject1/">
      AllowOverride all
      Require all granted
    </Directory>
</VirtualHost>

#
# Project 2
#

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/myProject2/"
    ServerName www.project2.dev
    ErrorLog "logs/project2-error_log "
    CustomLog "logs/project2-access_log" common
    <Directory "C:/wamp/www/myProject2/">
      AllowOverride all
      Require all granted
    </Directory>
</VirtualHost>

I also added them to my hosts file

127.0.0.1 http://project1.dev
127.0.0.1 http://project2.dev

But while I'm testing after restart wamp services, both http://project1.dev and http://project2.dev point at C:/wamp/www/myProject1/

The second path C:/wamp/www/myProject2/ related to project2.dev seems to be ignored.

Am I missing something?

Thanks.


Solution

You have not defined http://project2.dev anywhere - just http://www.project2.dev. So Apache defaults back to first config as it can't find a match.

Add the following config to the first vhost:

ServerAlias project1.dev

and similarly this to the second:

ServerAlias project2.dev

Then restart Apache.



Answered By - Barry Pollard
Answer Checked By - Marilyn (WPSolving Volunteer)