Ubuntu, apache2 wildcard dns to subdomain

apache-2.2mod-rewriteUbuntu

Currently I'm hosting my own (ubuntu) server with the following services: samba, ftp and a webserver. I've bought a domain and linked the DNS A-record to my ISP's IP. This is working correctly. Now I'd like to use the DNS wildcard-record to create subdomains. I want to avoid waiting 24hrs before the DNS change completes.

Thusfar I'm only able to redirect all incoming wildcards to the same directory:

test1.domain.com redirects to /var/www

test2.domain.com redirects to /var/www

Although I'd like to get:

test1.domain.com redirects to /var/www/test1

test2.domain.com redirects to /var/www/test2

My guess would be to change the file /etc/apache2/sites-available/domain.

Any help or tips would be welcome!

Thanks,

Mark

EDIT:

This is what my /etc/apache2/sites-available/domain file looks like:

<VirtualHost *:80>
        ServerAdmin webmaster@domain

        DocumentRoot /var/www/webroot
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/webroot>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Best Answer

You should be able to get the desired behavior with VirtualDocumentRoot.

Within your <VirtualHost>, add a ServerAlias to catch all the domains you're interested in:

    ServerAlias *.example.com

...then map them to the desired directories. Remove your DocumentRoot, and in its place, add:

    VirtualDocumentRoot /var/www/%1

You'll want to have a <Directory /var/www/> block allowing access, and keep in mind that this vhost should only be handling service for your dynamically configured vhosts - if you want example.com and www.example.com to be handled separately, then you'll want them to have their own <VirtualHost>.

Edit:

You'll want to use a different vhost to handle the "base" domains. Building off of the current config in the comment:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName catchall.example.com
    ServerAlias *.example.com
    # NOTE: this config will map test1.example.com to /var/www/test1
    VirtualDocumentRoot /var/www/%1
    # If you want that to map instead to /var/www/test1.example.com, then use %0:
    # VirtualDocumentRoot /var/www/%0
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Order Allow,Deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# This next vhost should be in a different file in sites-available to
# fit with debian-style vhosts - but make sure it's alphabetically
# after the file that contains the first vhost - we want it to load first
# so that it's default.  It can also just go in the same file.
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/www.example.com
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Order Allow,Deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Related Topic