Dynamic virtual hosting

Hi,

I want to dynamically achieve this:

www.domain.com -> /var/www/domain.com/htdocs
domain.com -> /var/www/domain.com/htdocs
sub.domain.com -> /var/www/domain.com/sub

I’ve started with this:

VirtualDocumentRoot /var/www/%-2.0.%-1.0/htdocs

Above solves the two first cases but it obviously doesn’t work in sub.domain.com case.

I’ve added all necessary DNS records (at least I think so) so that sub.domain.com resolves to the same ip as domain.com.

How should I solve this? I open to all suggestions involving mod_rewrite. Any help, examples would be much appreciated.

regards

//Morgan

Hi Morgan, welcome to SitePoint :wave:

This thread is also dealing with dynamic (sub)domains: http://www.sitepoint.com/forums/apache-configuration-199/can-i-have-domain-wide-wildcard-dns-742412.html

Albeit with CNAMEs instead of “real domains”; the principle is the same in both cases.

Have a look at that thread and see if it helps you. If not, feel free to come back with additional questions :slight_smile:

Ok, sorry but I’m a complete newbie in the world of Apache configurations. :confused:
Your suggestion, is it that I should look into ServerAlias? Would the idea be to alias www and non-www domains like this:
ServerAlias www.dynamicdomain.com dynamicdomain.com
so that they both point to the same path? Probably not since they already point to the same path, right?

Using the below I can handle www and non-www domains completely dynamic. I’ve tested this and it works:
VirtualDocumentRoot /var/www/%-2.0.%-1.0/htdocs

My issue is how to handle other subdomains like sub.dynamicdomain.com.

Can I set up a specific VirtualHost dealing with each specific subdomain and still keep the functionality of the line above? I mean like this:

<VirtualHost *:80>
ServerName sub.domain.com
VirtualDocumentRoot /var/www/domain.com/sub
VirtualScriptAlias /var/www/domain.com/cgi-bin
</VirtualHost>

VirtualDocumentRoot /var/www/%-2.0.%-1.0/htdocs
VirtualScriptAlias /var/www/vhosts/%-2.0.%-1.0/cgi-bin

Would this work?

Thanks for your help!

Just to get this straight, do you only have 1 sub-domain or an unknown amount, (probably supported by wilcard DNS) ?

If you have just the three: example.com, www.example.com and sub.example.com I would just create three virtual hosts and be done with. Configuring that dynamically is harder than just defining the vhosts, and vhosts are easier for Apache, too!

If however sub.example.com was an example and you can you have lots of different subdomains, all dynamic, I would create a vhost for the ones you know will never change (example.com and www.example.com) and

  1. use mass virtual hosting (VirtualDocumentRoot) to serve all the subdomains, OR
  2. use a default host that catches all uncaught request and let a script on that host handle the request

Does that make sense?

Sorry for not be clear :frowning:

I have multiple domains (hundreds) and just a few of them (10 or so) needs one specific subdomain.

So could I do <VirtualHost> for each specific sub.domain.com and then let mass virtual hosting handle the rest?

If so how would I do that, in what order should I place the parts?

Thanks, your help is very much appreciated!

//Morgan

[s]Something like this should work


NameVirtualHost 1.1.1.1:80
<VirtualHost 1.1.1.1:80>
        # VirtualHost for a known host
        ServerName example.com
        ServerAlias www.example.com sub.example.com sub2.example.com
        DocumentRoot /var/www/example.com/httpdocs
</VirtualHost>

# Optional more VirtualHosts for known hosts here

<VirtualHost 1.1.1.1:80>
        # And mass virtual hosting for all other hosts
        ServerName sub3.example.com
        VirtualDocumentRoot /var/www/example/%0/htdocs
</VirtualHost>

You should change it to fit your needs of course, but the principle works. Just tested it :)[/s]

Edit:

Doesn’t work :-\

Scratch the above, it doesn’t work at all :-/

Okay, so how about mass virtual hosting, where you just use %0 and then make hard links from one directory to another if it should be an alias?

Ok, I see.

Is there no other solution? I’ve read about hardlinks and I “dont like the feel of it”… I rather skip manually setting up hardlinks for all new domains that I add.

I thought this would be a rather common question with some clear solutions available. I mean subdomains are used quite often.

Thanks for any additional hints, ideas, solutions.

Heureka!

I think I’ve reached a working solution. Its based on ScallioXTXs example above that “didnt work”. I changed it a bit into this:

<VirtualHost *:80>
VirtualDocumentRoot /var/www/sites/%-2.0.%-1.0/www
VirtualScriptAlias /var/www/sites/%-2.0.%-1.0/cgi-bin
</VirtualHost>

<VirtualHost *:80>
ServerName sub.domain.se
DocumentRoot /var/www/sites/domain.se/test
VirtualScriptAlias /var/www/sites/domain.se/cgi-bin
</VirtualHost>

I now get the the results I wanted:

www.domain.com -> /var/www/sites/domain.se/www
domain.com -> /var/www/sites/domain.se/www
sub.domain.com -> /var/www/sites/domain.se/sub

Cool! And it’s all dynamic except that I have to add a “static” <VirtualHost> section for each subdomain I want.

Many thanks go out to ScallioXTX for assisting me with this!