Apache Virtual Host Configuration: Mapping Subpaths to Different Document Roots on Single Domain


2 views

When working with Apache virtual hosts, a common requirement is to serve different applications from subpaths of the same domain. The standard approach using Alias directives often leads to frustrating "Not Found" errors despite correct directory structures.

The original configuration:


Alias /test03/ /var/www/test03/public

fails because Apache doesn't automatically handle path translation for internal links and resources. When you request test06.com/test03/, Apache serves the directory, but subsequent requests for index.html fail because the browser requests /test03/index.html while the actual path is /var/www/test03/public/index.html.

Here's the proper virtual host configuration with necessary additions:



    ServerName test06.com
    ServerAlias www.test06.com
    DocumentRoot /var/www/test06/public

    # For test03 application
    Alias /test03 /var/www/test03/public
    
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    

    # For test04 application  
    Alias /test04 /var/www/test04/public
    
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    

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

After updating your configuration:

  1. Enable the new configuration: sudo a2ensite your-config-file.conf
  2. Reload Apache: sudo systemctl reload apache2
  3. Verify permissions: sudo chown -R www-data:www-data /var/www/test03/public

For more complex scenarios, consider URL rewriting:


RewriteEngine On
RewriteRule ^/test03/(.*)$ /var/www/test03/public/$1 [L]

Check these if issues persist:

  • Verify AllowOverride All is set for each directory
  • Check Apache error logs: tail -f /var/log/apache2/error.log
  • Confirm SELinux/apparmor isn't blocking access (for RedHat/Debian systems)

When working with Apache web server, you might encounter situations where you need to serve different websites from different directories under the same domain. For instance:

  • example.com/site1 should point to /var/www/site1/public
  • example.com/site2 should point to /var/www/site2/public

The initial approach many try is using Alias directives in the VirtualHost configuration, which often leads to issues like:

  • Directory listing appearing instead of the index page
  • "Not Found" errors when trying to access specific files
  • Broken relative paths in the served content

Here's a properly configured VirtualHost that solves these issues:


    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/main/public

    # Site1 configuration
    Alias /site1 /var/www/site1/public
    
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    

    # Site2 configuration
    Alias /site2 /var/www/site2/public
    
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    

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

Several critical components make this work:

  1. Directory Blocks: Each alias needs a corresponding Directory block with proper permissions
  2. Options -Indexes: Prevents directory listing when no index file is present
  3. AllowOverride All: Enables .htaccess files to work in the target directories

After making changes:

sudo apachectl configtest
sudo systemctl restart apache2

If you still encounter problems:

  • Check file permissions in the target directories
  • Verify SELinux contexts if applicable
  • Ensure mod_alias is enabled (a2enmod alias)

For more complex routing needs, consider using mod_rewrite:

RewriteEngine On
RewriteRule ^/site1(/.*)?$ /var/www/site1/public$1 [L]
RewriteRule ^/site2(/.*)?$ /var/www/site2/public$1 [L]