How to set up apache virtualhosts serving different directories?

8,868

I don't think you can do what you want to do. Apache httpd will server the first vhost that has a SeverName or ServerAlias that matches the host header in your request. If no vhost is explicitly matched then the first vhost defined will be served.

With the above in mind, the host header for all of your requests will be test.test so that matches your first vhost and apache httpd will attempt to serve the rest of the url from the relevant Documentroot.

You could try setting the document root for the first vhost one level higher. You could try to do something with mod_rewrite or even reverse proxies.

Share:
8,868

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    On my Ubuntu 16.04 system, I want to set up several websites that are served from several directories, that is, same domain, same port, same IP address. However, I don't know how to specify them. All instructions I could find cover setting up virtualhosts for different domains.

    That's how far I am right now:

    /etc/apache2/sites/sites-enabled/site1.conf:

    <VirtualHost *:80>
      ServerName test.test
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/vhosts/site1/
      <Directory /var/www/vhosts/site1/>
        AllowOverride None
      </Directory>
      ErrorLog /var/log/apache2/error.log
    </VirtualHost>
    

    /var/www/vhosts/site1/index.html

    <h1>site 1</h1>
    

    /etc/apache2/sites/sites-enabled/site2.conf:

    <VirtualHost *:80>
      ServerName test.test
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/vhosts/site2/
      <Directory /var/www/vhosts/site2/>
        AllowOverride None
      </Directory>
      ErrorLog /var/log/apache2/error.log
    </VirtualHost>
    

    /var/www/vhosts/site2/index.html

    <h1>site 2</h1>
    

    When I point my web browser to http://test.test,

    site 1

    is showing up in the browser.

    How/where can I specify that hxxp://test.test/site1 shall point to /var/www/vhosts/site1/(index.html) and hxxp://test.test/site2 shall point to /var/www/vhosts/site2/(index.html)?

    Bonus: hxxp://test.test and probably hxxp://test.test/site3 should point to somewhere else.