views:

46

answers:

2

Okay, this might have been answered before but apparently up until now I still haven't found the answer.

You may notice there are some websites that may allow users to register to instantly get a subdomain of their own in the website.

For example, the domain is www.domain.com. If I register a new user as henson, I will get my own page in the website, ex: www.henson.domain.com (not sure if the www part is necessary) So if a user open www.henson.domain.com, it will actually open www.domain.com?owner=henson

Can I do this using only htaccess? Because I read somewhere that this also needs manual creation of subdomains in cpanel (which defeats the purpose of the website).

Oh, the website is coded with flat PHP, so no MVC frameworks. IF someone knows how to do this easily with frameworks (preferably CodeIgniter), be welcome to answer.

Thanks for the answer.

+2  A: 

You cant in htaccess, you must setup a wild card virtual host and rewrite it to the URL/directory you require. See http://blog.orite.com.au/web_development/2009-01-22/setting-up-wildcard-virtual-hosts-for-web-development-environment/ for more info

Petah
`You cant in htaccess`. Actually... http://corz.org/serv/tricks/htaccess2.php (look for 'multiple domains in one root').
passcod
i've read that, and the code is like this:<VirtualHost *:80> VirtualDocumentRoot c:/projects/%1/</VirtualHost>where do i put this code? in htaccess?
Henson
In the `httpd.conf` or equivalent file (system dependent).
Petah
okay, that is if i try that in my localhost. So what about in hosting? And some websites can link purchased domain (for example purchased.com) to their main websites. how is it achievable?
Henson
@passcod, Actually, you can't. That method applies only if the web server is already setup to map both domains to one directory.
Petah
@Henson, same applies weather on your local web server or a remote web server. You will need access to the http configuration file.
Petah
update, I activated vhost_alias module, add <VirtualHost> line in the bottom of httpd.conf, restart wamp, then try project.localhost and failed. It redirects to http://www.project.localhost in firefox, and in chrome it simply displays no page found.
Henson
@Henson, You need to make sure `project.localhost` points to your web servers IP address. On a local Windows development web server you will need to edit `C:\Windows\System32\drivers\etc\hosts` and add `127.0.0.1 project.localhost`
Petah
@Petah : Ok, I get it. so how do i do it dynamically / automatically? I mean, each time, a user registers, there will be a new subdomain. From what you say, I have to input manually for every new subdomains right?
Henson
@Henson, once you have a DNS (or hosts file if running locally) pointing at the right IP/domain (you will need a wild card DNS record), it will be automatic. You will just need to create the right directory on register.
Petah
A: 

You can parse $_SERVER['SERVER_NAME'] to determine which subdomain is used. The www part is actually never necessary. This is simply a subdomain that has no meaning. Most of the time it is simply mapped to the main domain. Example:

if (preg_match('/^(www\.)?(.+)\.your-domain.com$/', $_SERVER['SERVER_NAME'], $matches) && $matches[2] != 'www') {
    $subdomain = $matches[2];
    // your logic goes here
}
elusive