Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What’s the best way to automate creation of subdomains (like Basecamp)?

My requirement is somewhat similar to Basecamp where users request for a subdomain from a page and I create it for them from my business logic.

Something like alt text

share|improve this question
up vote 7 down vote accepted

We use the same technique at our site. Our servers run Apache, MySQL and PHP on Linux.

Our DNS record contains a subdomain wildcard entry like EK already suggested:

www                      IN A       123.456.789.012
*                        IN CNAME   www

This will redirect all the requests to to the same ip as - it's also possible to have subdomains of subdomains.

I don't know perl, but in PHP you can access the host name at any time like this:


I am sure there is an equivalent in Perl to access the Host-Header of the HTTP-Request. We use a central PHP script file for this purpose.

Edit: Found some time to provide further details on handling of these domains.

You may save the complete subdomain as a primary key in your database. This guarantees the user entered domains to be unique. You may also use this info for availability checks during registration of your user.

I guess your application can handle multiple users via an user id or sommething like this. Just save this user id together with the subdomain in the database. Now you can lookup the user id by the current domain sent via the HTTP-Host header at any time.

share|improve this answer
In Perl, this is accomplished using CGI and the %ENV hash -- from CGI, you could access it like so: "my $host = $ENV{HTTP_HOST};" – Sam Halicke Nov 21 '09 at 19:12
Wonderful answer! I was wondering if there were any issues with this approach. – Quintin Par Nov 21 '09 at 20:07
this won't have much of an impact on performance. we've been using this approach for >1.5 years now and didn't encounter any issues so far – Zim Nov 21 '09 at 20:52
And if you have predefined subdomains you just store it in a for example - file, and exclude them in a PHP condition? – benqus May 16 '12 at 13:16

set a wildcard on your domain for * Then in code the used server name is in the header, simply read that to serve up customised content or redirect to a subfolder as required.

share|improve this answer

Your Answer


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.