Apache: Subdomain Management
January 24, 2008 1:31 AM

Quick Apachefilter Question: I have complete control over my setup. What is the best way to setup Apache to serve files from /home/domain.com/subdomains/of/level/arbitrary/an/ when an.arbitrary.level.of.subdomains.domain.com is requested? I cannot figure this simple one out for the life of me!

I'm using Apache 2 and have control over BIND, but I've already setup wildcard domains. Thankyou!
posted by PuGZ to Computers & Internet (8 answers total) 1 user marked this as a favorite
You probably need to use an external rewrite map script.

Something like this might work, but the final implementation is left as an exercise to the reader (i.e. I don't have the time to test this now...)

RewriteEngine on
RewriteMap sub prg:/usr/bin/subtopath.pl
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.net
RewriteRule ^/(.+)$ ${sub:%1}/$1 [L]

and the perl script something like:

#!/usr/bin/perl

$|++;

while(<>) {

if($_=~/(([^\.]+.)+)mydomain\.net/) {
print "/var/www/".join ( "/",reverse split /\./,$1)."/\n";
} else {
print "/var/www/default/\n";
}
}


The mod_rewrite documentation comes in really handy and probably has the answers you need to make this work.
posted by phax at 3:15 AM on January 24, 2008


Uh, the RewriteCond regex is wrong, that only takes one level of subdomain, ^(.*)\.mydomain\.net should be better. Sorry about that.
posted by phax at 4:27 AM on January 24, 2008


What would happen, if you have these examples:

Someone requests simple.sub.domain.com and sub.domain.com.
This would lead to
/home/domain/sub/simple
and the second subdomain to
/home/domain/sub
where you could see the directory "simpl"e as a subdirectory of his sub.domain.com.

Is that what you want?
posted by donut at 7:41 AM on January 24, 2008


From my experience at work, using a rewrite map script causes url corruption in 1-3% of requests in a high-load situation. The effect seems to be, the script gets called twice for two different requests, but overwrite each others responses and both requests get rewritten to the hash. It's sad because it's so useful.
posted by breath at 10:58 AM on January 24, 2008


donut: Yes, that is what I'm after!

phax: That looks very promising. I'll have a look into it after work today!

breath: What constitutes a high-load situation? Does it depend on the forking method used by Apache?
posted by PuGZ at 11:33 AM on January 24, 2008


Breath: Just some idle thoughts... maybe that sort of behaviour is related to the MPM in use? I'd imagine the threaded MPM wouldn't be good for this, but I'm willing to stand corrected.

But you are right, regardless of corruption, calling an external script is never efficient, and doing this more efficiently (mod_perl?) would probably be quite a lot of work.
posted by phax at 12:05 PM on January 24, 2008


...and I should really think before posting, but I've seem to have overlooked the RewriteLock parameter. For what it's worth, none of the live servers at work use mapping, so I can't really comment on it's performance but for toy servers it's been fine.
posted by phax at 12:15 PM on January 24, 2008


Whoops, sorry to not follow up sooner. The high-load site I'm talking about gets about 8 hits per second, average. We don't use the worker mpm, we use apache 1.3! Old-school!

Er, and I wasn't aware of RewriteLock's existence, and it appears that we don't use it in the conf files. Hm. I have informed the web team. You might have just saved 1-3% of our website, phax!

However, it seems that at least for a few it didn't fix things.
posted by breath at 1:55 AM on January 25, 2008


« Older Guitar repertoire recommendations   |   From pill-free to migraine-free Newer »
This thread is closed to new comments.