How can users create subdomains in real time?

Hi All,

I really like the simple sign-up process of, and I was wondering how to achieve the effect of allowing a new sign-up user to create a ready-to-go subdomain in real time?

To clarify, the registration process allows a new user to dynamically create a subdomain of their choice (if available) for the domain Seconds later, after the sign-up process has completed, you can visit your new website at http://<your chosen subdomain> V. nice.

I’m particularly interested because I’m toying with a website product/service offering (aren’t we all!) that would benefit from the same.

I’m assuming that this would require minor on-the-fly modifications to http.conf and/or the zone file for the domain, plus almost certainly a re-read/load of (at least) httpd.conf immediately after the necessary changes have been applied.

Just to complicate things a little further :slight_smile: , I’d like that all the (potentially many) subdomains that users create are dynamically pointed/parked over a sub-folder of the primary domain, eg.:

Big ask I know, but I’d be grateful for any hints or guidance whatsoever. I know my way fairly well around http.conf, and I currently use a WHM/cPanel interface to perform minor (ie. non-adventurous) editing of DNS zone files. This of course is on a dedicated server (hosted incidentally by those very nice people over at Ventures Online who also host this forum) to which I have root access.

Thanks v. much.

Setup wildcard subdomains on your server, then create a script called by mod_rewrite that redirects to the proper directory when a request is made for a subdomain. Then all you have to do is create directories.

That’s just the kind of kick-start I was looking for - thanks Wayne :slight_smile:

The Apache Documentation Provides a general overview:

Other Options include:

If you are seriously interested in mod_vdbh, I have a re-written version of it that uses libdbi. It can also dynamicly set the PHP safe_mode and open_basedir. I just haven’t gotten around to releasing the source code.


Thanks - I also got more help from Pippo over here:

I am Nikhil(username:nikhil_dec) and have started working in PHP in recent times. Please tell me clearly how we can create dynamic subdomains.If possible tell me all the steps clearly as i m new to this dynamic subdomain concept and have little knowledge of server access.

Hi Wayne,

I’m looking to do exactly what “spaceman” described. I had my host set up the wildcard
DNS for me and I’ve been working on the stuff in httpd.conf. Does this code look like it would work?

DocumentRoot “/home/owner/website/htdocs”
ScriptAlias /cgi-bin/ /home/owner/website/cgi-bin/
LogLevel emerg
CustomLog /home/owner/website/logs/access_log “combined”
ErrorLog /home/owner/website/logs/error_log
LimitRequestBody 15360000
Options +FollowSymLinks
RewriteEngine on
RewriteOptions maxredirect=3
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond ^www\.domain\.com/users/%1 !-d
RewriteRule ^([^.]+)\.domain\.com(.
) /home/owner/website/htdocs/users/$1/$2 [L]

The script that creates a new user account will ask for the subdomain name and
create a folder for it as: /home/owner/website/htdocs/users/subdomain Then it
gives the new user this URL to point to their folder:

My goal here is to allow users to type their (subdomain) URL into their browser and
be sent to the index page or other files in their folder while making the URL in the address
bar of their browser show:

The first RewriteCond should (I hope) check for a subdomain in the request.
The second one should skip rewriting if they typed:
The third one should make sure that a folder named /users/subdomain exists.

Then the RewriteRule should do the rest.

Clearly, what I want is pretty much the same as what does. I’ve read a bazillion forum posts, all the Apache onlione docs and I must admit I’m still uncertain
whether I’m on the right track. But getting this to work as described is mission-critical.

Also, will I need to restart Apache so it will re-read httpd.conf every time a new subdomain
and its associated folder is created? I could do that from the signup script if I need to, by
sending a shell command to the server. But that makes me nervous about security.



From what I’ve read about dynamic virtual hosts, you’ve got it! I would have done the rewrite things just a little differently but, if it works, DON’T fix it!

No, you will NOT have to restart Apache when new subdomains are added. My only concern would be to be sure that a new directory is created (without conflict with an existing one) AND POPULATED with a default file (index.html?) by your subdomain generating script.




Thanks. I made a few tweaks to the stuff I put in the Apache config file (htttpd.conf)
and got exactly the behavior I wanted.

When a subdomain is generated, a directory is created for it and populated with
an index file and all other necessary stuff. The names of the subdomains/directories
are stored in a MySQL database. All subdomains are forced to be unique within the
system. We even ban objectionable subdomain names so that nobody can use them.

I’ll drop by later and let you know about the site I’m setting up. All I can say right now is
that if the word “podcasting” means anything to you, the new site will knock your socks off.



Glad that it’s Party Time! :Partier:

Keep us posted - I’ve created RSS files to allow a client’s 27Mb interview files to be downloaded when “offline” but I’d like to see something more reasonable working, too.



The code is gr8. But my problem is that since my site is in shared server, I can’t alter the httpd.conf file. So, Can we achieve the above functionality using .htaccess file as we do for other mod_rewrite??
If it can be done, then plz could u plz modify and post back the above code for the .htaccess file?

Hi David,

This code in httpd.conf works fine for redirecting URLs like’ to ‘/member/business’

php_flag allow_url_fopen 1
DocumentRoot “/home/owner/”
ScriptAlias /cgi-bin/ /home/owner/
LogLevel emerg
CustomLog /home/owner/ “combined”
ErrorLog /home/owner/
LimitRequestBody 102400000
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^blog\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com [NC]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}/member/%2 !-d
RewriteRule ^(.
)$ /member/%2/$1
RewriteRule ^/load/(.*)$ /load.php?f=$1 [L]

When you type in the SEF URL, you get the home page for the
subdomain and the URL in the address bar says

So far, so good. (The subdomain’s folder was created when the
member signed up.)

But the second RewriteRule isn’t working. I think it’s being ignored.

It’s intended to redirect: to:

But it doesn’t work; I just get a 404 error instead, claiming the
server can’t find /load/goodMusic.mp3

The script load.php serves to get real-time tracking for the
goodMusic.mp3 file (and some other goodies we need), then
uses a header(“Location: goodMusic.mp3”) call to deliver
the file.

We can’t use this:
in the RSS feed because it’s not compatible with iTunes.

How can I get this to work?

Thanks in advance,

P.S. I hope my use of an actual URL isn’t seen as a plug; I
had promised you earlier I’d let you know where the site


The second RewriteRule is part of the first rule and it’s conditions - you forgot to use the Last flag ([L]) to terminate that set of conditions and rules. It should be as simple as that!



Hi David,

I added the [L] flag to the first RewriteRule and restarted Apache.
When I did that, none of the subdomains would come up. I took it
out, restarted and it was back to “normal”. Of course, I’m still not
getting the effect the second rule was supposed to provide.


This is what I think you should try.

# You can put first and second line into one
RewriteCond %{HTTP_HOST} !^(blog|www)\\.domain\\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.domain\\.com [NC]

# I don't think this RewriteCond is working. Remove and test, again
#RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}/member/%2 !-d
# Maybe this is what you want ... but probably you don't need it
#RewriteCond %{DOCUMENT_ROOT}/member/%2%{REQUEST_URI} -d

# Modified to avoid endless looping
RewriteRule !^/*member/ /member/%2%{REQUEST_URI}

# I'm not sure if the slashe is there in httpd.conf. Maybe.
# You may want to try this.
#RewriteRule ^/*load/(.*)$ /load.php?f=$1 [L]

If you continue to experience problem,
I’d suggest checking and showing ErrorLog, and using RewriteLog.

You can.


Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com [NC]
RewriteRule !^/*member/ member/%2%{REQUEST_URI}

This code will take URL like this:

and rewrite to:

There are other ways, too.
It depends on what you want.

But you should be aware that these type of rewrite may disturb some script (mombo, for example).

Thanks extras,

I tried those suggestions but I’m back to sqaure one. I still get the subdomain
home pages to load but trying to load/goodMusic.mp3 gives me a 404 error.

I got this from the RewriteLog:
init rewrite engine with requested uri /load/Billy_Bragg_and_Wilco_-California_Stars.mp3 - - [31/Aug/2005:19:53:47 --0500] [][rid#970d650/initial] (2) rewrite /load/Billy_Bragg_and_Wilco
-California_Stars.mp3 -> /podcast/programmer/load/Billy_Bragg_and_Wilco-California_Stars.mp3 - - [31/Aug/2005:19:53:47 --0500] [][rid#970d650/initial] (2) local path result: /podcast/programmer/load/Billy_Bragg_and_Wilco
-California_Stars.mp3 - - [31/Aug/2005:19:53:47 --0500] [][rid#970d650/initial] (2) prefixed with document_root to /home/rrumford/
-California_Stars.mp3 - - [31/Aug/2005:19:53:47 --0500] [][rid#970d650/initial] (1) go-ahead with /home/rrumford/
-_California_Stars.mp3 [OK]

It looks like the last rule just isn’t being used at all.


I need to see more than that.
Especially, the output from the failing URL.
but I want to see the output of working URL, as well.
(RewriteLog may make big output)

How about the error log?
What is the version of Apache/OS?
Are there .htaccess?

Hi extras,

Here’s the output from a working URL: - - [31/Aug/2005:20:31:13 --0500] [][rid#97d0648/initial] (2) init rewrite engine with requested uri /podopera_Sept_05.mp3 - - [31/Aug/2005:20:31:13 --0500] [][rid#97d0648/initial] (2) rewrite /podopera_Sept_05.mp3 -> /podcast/podopera/podopera_Sept_05.mp3 - - [31/Aug/2005:20:31:13 --0500] [][rid#97d0648/initial] (2) local path result: /podcast/podopera/podopera_Sept_05.mp3 - - [31/Aug/2005:20:31:13 --0500] [][rid#97d0648/initial] (2) prefixed with document_root to /home/rrumford/ - - [31/Aug/2005:20:31:13 --0500] [][rid#97d0648/initial] (1) go-ahead with /home/rrumford/ [OK]

Note that this is delivered directly out of the subdomain folder. It’s not going
through /load/podopera/podopera_Sept_05.mp3 or into

I’ve got RewriteLogLevel set at 2. I’m not getting any error stuff in the
RewriteLog. How high should I set it to make it show some errrors? I don’t want
to slow down the server too much; this is a live system.

The error log has 0 bytes - probably because the site is set to loglevel emergency.
This host just never screws up anything so there have been no really serious
errors to report.

The lines I showed you from RewriteLog were a failing URL. It claimed it was
internally redirecting, but the only output was a 404 error. I think that’s because
it was internally redirecting only to the extent of looking for /load/etc. in the
subdomain folder.


Hi extras,

I forgot to give you the apace version; it’s 2.0x