Apache2 – Setting Up VirtualHost Auto Subdomain

apache-2.2virtualhostwildcard

I just got myself a brand new Linux-server (Debian Stable). The server is primarily used for webhosting and webdevelopment.

I installed Apache2, PHP5 and MySQL and it all works.

The way I organize my sites, is with the following path: /var/www/username/website.com

Now I wan't the server to automatically "make"/recognize subdomains.

/var/www/username/sub.website.com > http://sub.website.com

I wan't the above to happen automatically. How to do that?

Best Answer

The best way to do it would be to put links to all sites in a specific directory and do something like this :

<VirtualHost *:80>
        RewriteEngine On
        RewriteMap lowercase int:tolower
        # if already rewitten and we have the right path, stop right here
        RewriteRule ^(/where/your/sites/are/[^/]+/.*)$ $1 [L]
        RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
        RewriteRule ^(www\.)?([^/]+)/(.*)$ /where/your/sites/are/$2/$3 [L,E=VHOST_ROOT:/where/your/sites/are/$2/]
</VirtualHost>