How can users create subdomains in real time?


#1

Hi All,

I really like the simple sign-up process of blogger.com, 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 blogger.com registration process allows a new user to dynamically create a subdomain of their choice (if available) for the domain blogspot.com. Seconds later, after the sign-up process has completed, you can visit your new website at http://<your chosen subdomain>.blogspot.com. 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 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.


#2

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.


#3

That's just the kind of kick-start I was looking for - thanks Wayne smile


#4

The Apache Documentation Provides a general overview:
http://httpd.apache.org/docs-2.0/vhosts/mass.html

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.

-chipux


#5

Thanks - I also got more help from Pippo over here: http://www.sitepointforums.com/showthread.php?t=137704


#6

hi,
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.
thanx
nikhil


#7

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?

<VirtualHost 25.33.56.41>
DocumentRoot "/home/owner/website/htdocs"
ServerName www.domain.com
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
ServerAlias *.domain.com
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]
</VirtualHost>

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:

http://subdomain.domain.com

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:

http://subdomain.domain.com/path/to/somefile

The first RewriteCond should (I hope) check for a subdomain in the request.
The second one should skip rewriting if they typed: http://www.domain.com
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 blogger.com 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.

TIA


#8

kaspar,

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.

Regards,

DK


#9

dklynn,

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.

kaspar


#10

kaspar,

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.

Regards,

DK


#11

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?


#12

Hi David,

This code in httpd.conf works fine for redirecting URLs like
'http://business.podblaze.com' to '/member/business'

<VirtualHost aa.bb.cc.dd:80>
php_flag allow_url_fopen 1
DocumentRoot "/home/owner/domain.com/htdocs"
ServerName www.domain.com
ScriptAlias /cgi-bin/ /home/owner/domain.com/cgi-bin/
LogLevel emerg
CustomLog /home/owner/domain.com/logs/access_log "combined"
ErrorLog /home/owner/domain.com/logs/error_log
ServerAlias *.domain.com
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]
</VirtualHost>

When you type in the SEF URL, you get the home page for the
subdomain and the URL in the address bar says
http://business.podblaze.com

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:
http://business.podblaze.com/load/goodMusic.mp3 to:
/member/load.php?f=goodMusic.mp3

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,
Kaspar

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
was...


#13

kas,

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!

Regards,

DK


#14

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.

Kas


#15

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.


#16

You can.

Example:

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:
http://user1.example.com/subdir/afile.html

and rewrite to:
DOCUMENT_ROOT/member/user1/subdir/afile.html

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).


#17

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_-CaliforniaStars.mp3
24.225.65.157 - - [31/Aug/2005:19:53:47 --0500] [programmer.podblaze.com/sid#9589cc8]rid#970d650/initial rewrite /load/Billy_Bragg_and_Wilco_-CaliforniaStars.mp3 -> /podcast/programmer/load/Billy_Bragg_and_Wilco_-CaliforniaStars.mp3
24.225.65.157 - - [31/Aug/2005:19:53:47 --0500] [programmer.podblaze.com/sid#9589cc8]rid#970d650/initial local path result: /podcast/programmer/load/Billy_Bragg_and_Wilco_-CaliforniaStars.mp3
24.225.65.157 - - [31/Aug/2005:19:53:47 --0500] [programmer.podblaze.com/sid#9589cc8]rid#970d650/initial prefixed with document_root to /home/rrumford/podblaze.com/htdocs/podcast/programmer/load/Billy_Bragg_and_Wilco_-CaliforniaStars.mp3
24.225.65.157 - - [31/Aug/2005:19:53:47 --0500] [programmer.podblaze.com/sid#9589cc8]rid#970d650/initial go-ahead with /home/rrumford/podblaze.com/htdocs/podcast/programmer/load/Billy_Bragg_and_Wilco_-CaliforniaStars.mp3 [OK]

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

Kaspar


#18

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?


#19

Hi extras,

Here's the output from a working URL:

66.41.123.226 - - [31/Aug/2005:20:31:13 --0500] [podopera.podblaze.com/sid#9648cc8]rid#97d0648/initial init rewrite engine with requested uri /podopera_Sept_05.mp3
66.41.123.226 - - [31/Aug/2005:20:31:13 --0500] [podopera.podblaze.com/sid#9648cc8]rid#97d0648/initial rewrite /podopera_Sept_05.mp3 -> /podcast/podopera/podopera_Sept_05.mp3
66.41.123.226 - - [31/Aug/2005:20:31:13 --0500] [podopera.podblaze.com/sid#9648cc8]rid#97d0648/initial local path result: /podcast/podopera/podopera_Sept_05.mp3
66.41.123.226 - - [31/Aug/2005:20:31:13 --0500] [podopera.podblaze.com/sid#9648cc8]rid#97d0648/initial prefixed with document_root to /home/rrumford/podblaze.com/htdocs/podcast/podopera/podopera_Sept_05.mp3
66.41.123.226 - - [31/Aug/2005:20:31:13 --0500] [podopera.podblaze.com/sid#9648cc8]rid#97d0648/initial go-ahead with /home/rrumford/podblaze.com/htdocs/podcast/podopera/podopera_Sept_05.mp3 [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
/load.php?f=podopera/podopera_Sept_05.mp3

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.

Kaspar


#20

Hi extras,

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