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.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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>
share|improve this answer
    
I can't put all the sites in one directory - it is important that every user has a folder with their own sites. –  Niklas H Feb 14 '10 at 15:02
1  
That's why I said "links to all the sites" :-) –  mat Feb 14 '10 at 16:52
    
Sorry - I didn't see that link part. I read it as "put all the sites in a specific directory" :) –  Niklas H Feb 14 '10 at 20:40
add comment

You can use mass virtual hosting. In your case you will not be able to use it in the main apache configuration because of '/username/' in the path but you can create a virtualhost per user then use mass virtual hosting in the virtual host.

Example:

<VirtualHost *:80>
ServerName domain.tld
ServerAlias *.domain.tld
VirtualDocumentRoot /var/www/user1/%0/
</VirtualHost>

<VirtualHost *:80>
ServerName domain2.tld
ServerAlias *.domain2.tld
VirtualDocumentRoot /var/www/user2/%0/
</VirtualHost>

...

With mod rewrite you can them make domain.tld to rewrite to point to the www directory

share|improve this answer
    
How would such a rewrite look like? –  Niklas H Feb 14 '10 at 15:01
add comment

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.