Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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>
share|improve this answer
    
Thanks for your respond! I've been able to configure apache2 VirtualHost with the given lines. Although it ain't working like it should. Whenever browsing to www.example.com or example.com leads to seperately directories, which i don't want - they need to link to the same directory. Basically, I don't get your following line: "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". Could you give me a last hand with this? Thanks! –  Velth Aug 30 '12 at 7:02
    
This is how my /etc/apache2/sites-available/domain file looks like: snipt.org/vDjj2 –  Velth Aug 30 '12 at 7:18
    
Can you clarify how you're looking to configure the www.example.com and example.com domains? They're not the same domain, so having them serve the same content doesn't fit with having the dynamic virtual docroot determine the directory automatically. Do you want them to be a special case where they serve the same content, or do you want one to redirect to the other? –  Shane Madden Aug 30 '12 at 7:23
    
I'd like to achieve subdomains so I can avoid creating DNS A-records for every subdomain, therefor I'd like to use the wildcard DNS record. Both www.example.com and example.com should point to the same directory: /var/www/www.example.com. For subdomains it would be: sub.example.com whereas the directory: /var/www/sub.example.com. –  Velth Aug 30 '12 at 7:33
    
@MarkvanVelthoven Edited my answer with info on how to make that happen. –  Shane Madden Aug 30 '12 at 7:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.