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.
Source: Tips4all
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
ReplyDeleteIt's nothing to do with .htaccess. You'll need to set up DNS records and virtual hosting for the subdomains.
ReplyDeleteYou could allow every subdomain in the first place and then check if the subdomain is valid. For example:
ReplyDeleteRewriteEngine 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.
Mod_vhost_alias is the right module to do this.
ReplyDeleteWith 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
I think the wild card DNS with Apache's Dynamic Mass Virtual Hosting is a reasonable solution also. Although, I have never tried it.
ReplyDeleteIf 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.