Home > front end >  Configuring multiple sites in apache
Configuring multiple sites in apache

Time:10-17

I use Win10, Apache2.4 and PHP7.4. I created 2 test sites inside Apache24\htdocs - Test.com and Test2.com, both containing simple index.php files. Here are exceptions from config files:

hosts:

    127.0.0.1 Test.com  www.Test.com
    127.0.0.1 Test2.com www.Test2.com

httpd.conf:

    # PHP7 module
    PHPIniDir "C:/Dev/PHP-7.4.24"
    LoadModule php7_module "C:/Dev/PHP-7.4.24/php7apache2_4.dll"
    AddType application/x-httpd-php .php

httpd-vhosts.conf:

    <VirtualHost _default_:80>
        DocumentRoot "${SRVROOT}/htdocs"
        ServerName www.example.com:80
    </VirtualHost>

    <VirtualHost *:80>
        DocumentRoot "${SRVROOT}/htdocs/Test.com"
        ServerName Test.com
        ServerAlias www.Test.com
    </VirtualHost>

    <VirtualHost *:80>
        DocumentRoot "${SRVROOT}/htdocs/Test2.com"
        ServerName Test2.com
        ServerAlias www.Test2.com
    </VirtualHost>

When loading both test.com and test2.com, browser shows folder index of htdocs. Only clicking on either index shows actual site, and browser address field become, for example, http://test.com/Test.com/. How to configure apache to show site from address like test.com?

CodePudding user response:

<VirtualHost *:80>

</VirtualHost

hey apache if you get any request on port 80 go to folder test.com

hey apache if you get any request on port 80 go to folder test2.com

doh !

so if i make a localhost request http://127.0.0.1:80 apache should just magically know which folder to go to right ???

CodePudding user response:

When loading both test.com and test2.com, browser shows folder index of htdocs.

Ths will happen if the ServerName (or ServerAlias) directives do not match the requested Host header on the request. It will then default to the first <VirtualHost>.

ServerName Test.com
ServerAlias www.Test.com

I wouldn't necessarily expect this to be case-sensitive, but you've defined the ServerName as Test.com (capital T), but you are requesting test.com (all lowercase) - the browser automatically lowercases the hostname in the HTTP request anyway, so you can't actually make a request for Test.com.

You should be defining the ServerName as all lowercase.

ServerName test.com
ServerAlias www.test.com
127.0.0.1 Test.com  www.Test.com
127.0.0.1 Test2.com www.Test2.com

Likewise, this should also be lowercase.

Only clicking on either index shows actual site, and browser address field become, for example, http://test.com/Test.com/

That looks like you are incorrectly linking to the /Test.com subdirectory, not the http://test.com host?

I tried to fully comment out this block - nothing changed.

Do you have a similar default in the main server config, outside of a <VirtualHost> container?

  • Related