Home > Mobile >  Apache2 - Set Document Root based on subdirectory
Apache2 - Set Document Root based on subdirectory

Time:11-06

I have a default Virtual Host. It is configured to watch Document Root inside /var/www/html and works great. But I have a problem. I have multiple sites inside subdirectories and I don't know how to set Document Root for each of them. For example, /var/www/html/test, /var/www/html/test2, etc. When I include file from /var/www/html/test, it searches inside var/www/html. For example, I include file like include_once '/core.php', but I have an error Failed opening required '/var/www/html/core.php', because there is no current file here. Is it possible to set Document Root for each of directories inside main Virtual Host? Thank you!

My main Virtual Host configuration:

<VirtualHost *:80>
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    AccessFileName .htaccess

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Also, you can say, that I need to use relative paths, but it isn't possible in some ways, because, sometimes, I work with ready sites, created by another people.

CodePudding user response:

Is it possible to set Document Root for each of directories inside main Virtual Host?

Short answer is no, you cannot. 1 Document Root per Virtual Host. In order to understand how the things are working you need to understand what is the directives context. Lets take as an example VirtualHost directive. As we can see from the docs it has only 1 Context which is Server Config. And server config context tells next:

directive may be used in the server configuration files (e.g., httpd.conf), but not within any VirtualHost or Directory containers. It is not allowed in .htaccess files at all.

But DoocumentRoot has two contexts: server config and virtual host. Rest you should figure out for yourself ;)

There are plenty ways how you can achieve desired result

  1. Running several name-based web sites on a single IP address
  2. Running different sites on different ports
  3. Mixed port-based and ip-based virtual hosts

Hope that this answer will help you!

  • Related