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.
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:
This will redirect all the requests to x.y.yourdomain.com to the same ip as www.yourdomain.com - 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.
set a wildcard on your domain for *.yourdomain.com. 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.