Configuring Apache: Mapping Subdomains to Directories in the Filesystem

Apache VirtualDocumentRoot Configuration

The Apache feature called VirtualDocumentRoot, combined with the wildcard DNS setting (likely already configured for the *.be domain), enables dynamic hosting of subdomains. Here's how it works:

Configuration Explanation

1. VirtualHost

<VirtualHost 176.9.220.33:80>

This directive tells Apache to listen for incoming HTTP traffic on IP address 176.9.220.33 and port 80.

2. ServerAlias

ServerAlias *.be

The ServerAlias directive ensures that all subdomains of .be (e.g., test..be) are handled by this VirtualHost configuration.

3. VirtualDocumentRoot

VirtualDocumentRoot /var/www/static/be_/subdomains/%1

%1 is replaced by the first part of the subdomain. For example:

  • ..be%1 = immofrancois
  • test..be%1 = test

Apache automatically uses the directory /home/admin/static/be_/subdomains/immofrancois as the document root for http://..be without additional scripts.

Redirect All HTTP Requests

<VirtualHost 176.9.220.33:80>
    ServerName .be
    ServerAlias *.be

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R=301,L]

    LogLevel warn
    ErrorLog /var/log/apache2/_http_error.log
    CustomLog /var/log/apache2/_http_access.log combined
</VirtualHost>

HTTPS Configuration

<VirtualHost 176.9.220.33:443>
    ServerName .be
    ServerAlias *.be

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/leads/.crt
    SSLCertificateKeyFile /etc/apache2/ssl/leads/.key
    SSLCertificateChainFile /etc/apache2/ssl/leads/.crt

    AddDefaultCharset utf-8

    VirtualDocumentRoot /home/admin/static/be_/subdomains/%1

    <Directory /home/admin/static/be_/subdomains/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    Alias /static/ /var/www/be_/static/
    <Location "/static">
        SetHandler None
        Options -Indexes
        AddType text/html php
    </Location>

    Alias /media/ /var/www/be_/media/
    <Location "/media">
        SetHandler None
        Options -Indexes
        AddType text/html php
    </Location>

    LimitRequestBody 5120000

    LogLevel warn
    ErrorLog /var/log/apache2/_https_error.log
    CustomLog /var/log/apache2/_https_access.log combined
</VirtualHost>

Debugging 404 Errors on Root https://.be

LogLevel trace4
ErrorLog /var/log/apache2/a_https_error.log
CustomLog /var/log/apache2/_https_access.log "%v:%p %h %l %u %t \"%r\" %>s %O file=%f"

Apache attempts to load files from /home/admin/static/be_/subdomains/ instead of /home/admin/static/be_/main, causing 404 errors.

Possible Causes:

  • VirtualDocumentRoot in use: This setting automatically maps subdomains to subdirectories.
  • ServerAlias or ServerName conflict: The wildcard alias *..be might redirect traffic to an unintended directory.
  • RewriteRules: Active mod_rewrite rules could be altering file paths.

Solution:

Create a directory and place an index.html file there to avoid modifying the configuration.

ln -s /home/admin/static/be_/subdomains/index.html /home/admin/static/be_/subdomains/www/index.html
ls -l /home/admin/static/be_/subdomains/www

This symbolic link ensures that changes in the original directory are reflected in the web-accessible directory, reducing redundancy.

Comments