views:

134

answers:

2

Possible Duplicate:
Create subdomain upon user registration

Suppose i have a site and i wish to give a sub-domain for each registered users. like my site http://site.com/

and the test-user is a user registered on my site and site want to make sub-domain for that user.

Like http://test-user.site.com

Like http://test-user1.site.com for test-user1.

Hop you understood the requirement. How can i create a sub-domain using my sites back-end. or dynamically while register

+6  A: 
  1. make a DNS record to bring * requests to the site IP.
  2. set up a web server to handle * requests too (thanks @animuson for mentioning)
  3. check $_SERVER['HTTP_HOST'] against database.
  4. Profit!!!
Col. Shrapnel
You forgot to mention modifying the VirtualHost for the domain to catch those * requests being brought to the IP.
animuson
This is terrible for performance - every request for every resource has to be run through PHP this way. Not a good option for real-world use IMO (although all the alternatives I can think of involve fiddling with the server's `VirtualHost` configuration and are often impossible).
Pekka
Who orders you to request from this subdomain any resource other than html, @Pekka ?
Col. Shrapnel
@Pekka: You can always specify certain subdomains that will go directly to certain files or folders and skip PHP, as long as they are defined before the wildcard VirtualHost in the configuration. That's how I run my site. Apache directs everything to my index.php file, except for the resource subdomains for css, images, scripts, etc which all go directly to their respective folders.
animuson
@Col @animuson this method is fine as long as you yourself are the only people running the site. The OP, however, is looking to set up a site for each of his users, who are unlikely to be willing to share a resources folder.
Pekka
@Pekka I can't see in the question any sign of the OP's intention to become an ISP for his users. However it can be issue too. But the general use is just to substitute site.com/user with user.site.com and there is no problem in such case
Col. Shrapnel
+1  A: 

This is actually a lot simpler than it seems. The first thing you're going to do is setup a wildcard dns entry, which will send all traffic to for those subdomains to your site. Without this all of your subdomains will be unreachable.

From there you have a few different options-

Through your webserver configurtion (virtual host, .htaccess file, whatever) you can direct different subdomains internally to different folders.

You could also use a script and check against the request itself ($_SERVER['HTTP_HOST'] in php) to find the subdomain, and then do whatever customization you need that way.

tedivm