views:

3878

answers:

9

I am looking to create a system which on signup will create a subdomain on my website for the users account area.

e.g. johndoe.website.com

I think it would be something to do with the .htaccess file and possibly redirecting to another location on the website? I don't actually know. But any information to start me off would be greatly appreciated.

Creating a sign up area is not the problem - i have done this many a time. i am just unsure where to start with the subdomain.

Thanks, Ben.

+4  A: 

It's nothing to do with .htaccess. You'll need to set up DNS records and virtual hosting for the subdomains.

chaos
+4  A: 

You could allow every subdomain in the first place and then check if the subdomain is valid. For example:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$
RewriteRule !^index\.php$ index.php [L]

Inside the index.php you can than extract the subdomain using:

if (preg_match('/^([^.]+)\.example\.com$/', $_SERVER['HTTP_HOST'], $match)) {
    var_dump($match[1]);
}

But all this requires that your webserver accepts every subdomain name.

Gumbo
Moreover, it requires that your DNS server resolves every subdomain name.
chaos
I have seen this done on other websites, surely they dont have to resolve every subdomain everytime do they?
Ben McRae
Well, it depends on what you mean by that. If you mean, every time someone on the internet looks up foo.somedomain.com and it isn't cached, their DNS server has to resolve it, then yes.
chaos
If you mean, do they have to set up DNS records specifically for each domain, see Treffynnon's answer.
chaos
okay thanks for that. i dont quite understand what Treffynnon means by specifying the wildcard in the vhost?
Ben McRae
+17  A: 
  1. You need to create a wildcard domain on your DNS server *.website.com
  2. Then in your vhost container you will need to specify the wildcard aswell *.website.com - This is done in the ServerAlias http://httpd.apache.org/docs/1.3/mod/core.html#serveralias
  3. Then extract and verify the subdomain in PHP and display the appropriate data
Treffynnon
+3  A: 

In addition to setting up a DNS wildcard, you might want to take a look at Dynamic Mass Virtual Hosting for Apache which is how I've solved this in the past

Rowland Shaw
A: 

I think the wild card DNS with Apache's Dynamic Mass Virtual Hosting is a reasonable solution also. Although, I have never tried it.

If you have the need to scale out to multiple servers or the other solutions just don't work for you, I recommend using a database driven DNS server. I have successfully used MyDNS in the past. Since it uses MySQL (or PostgreSQL) you can update your DNS on the fly with PHP or just about anything else. The code doesn't look like it has been updated in a while, but it's DNS and therefore not exactly cutting edge.

Barrett Conrad
+1  A: 

The easiest way is to redirect all subdomains (with wildcard *) to point to your /wwwroot. Then put .htaccess to this folder with the following code:

RewriteCond %{ENV:REDIRECT_SUBDOMAIN} ="" 
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.com\.?(:80)?$ [NC] 
RewriteCond %{DOCUMENT_ROOT}/%1 -d 
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L] 
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

This will accomplish that every subfolder of the /wwwroot folder in acceptable via subdomain (foldername.domain.com).

Found this years ago on http://www.webmasterworld.com/apache/3163397.htm

glavić
A: 

Glavić's method definitivaly worked for me...

Thank you!

+2  A: 

Mod_vhost_alias is the right module to do this.

With one line you can tell Apache to look at the right place, with directory hashing, etc. For example, the line:

VirtualDocumentRoot /http/users/%3.1/%3.2/%3

would tell Apache to set the document root to /http/users/s/u/subdomain when requested for subdomain.yourdomain.com

Julien Tartarin
A: 

Thanks for help. I will use it for my site.

Best Free Ads