Dynamic Subdomain PHP
Asked Answered
H

4

8

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.

Handyman answered 20/10, 2010 at 9:50 Comment(2)
check this one out #586629Sublingual
See my solution. Provided you can enable wildcard subdomains, this can be achieved in .htaccess.Faraday
S
5

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

Sculptor answered 20/10, 2010 at 9:52 Comment(12)
You cant in htaccess. Actually... corz.org/serv/tricks/htaccess2.php (look for 'multiple domains in one root').Mind
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?Handyman
In the httpd.conf or equivalent file (system dependent).Sculptor
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?Handyman
@passcod, Actually, you can't. That method applies only if the web server is already setup to map both domains to one directory.Sculptor
@Henson, same applies weather on your local web server or a remote web server. You will need access to the http configuration file.Sculptor
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 project.localhost in firefox, and in chrome it simply displays no page found.Handyman
@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.localhostSculptor
@Sculptor : 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?Handyman
@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.Sculptor
@FélixSaparelli actually, this can be achieved using .htaccess. See my solution.Faraday
"Actually" my comment says the same thing. You probably wanted to target your comment to someone else.Mind
P
4

If you have CPANEL on your server, there is an XMLAPI which allows you to dynamically creates subdomains through PHP.

Yes, dynamically, not manually. I just spent the last 2 days on this (dynamic creation of everything from subdomains to email accnts to addon domains and sql dbs, users....everything), and cpanel API handles it all, cleanly. So take a while and figure it out.

Download the XMLAPI at the first link on this page: http://forums.cpanel.net/f42/xml-api-php-class-version-1-0-a-136449.html. The file xmlapi.php is the only one you need on your server.

That forum page is a nightmarish graveyard of half working examples written by very advanced and/or very hacky coders with no clear starting point.

Here is a basic script in PHP to add subdomains, replace the caps w your personal values. This took me quite a while to get right. Best of luck! Next steps, hit that forum link and read all the other API1 and API2 functions!

include("PATH_TO_THE_DOWNLOADED_xmlapi.php");

    $ip = "YOUR_IP_ADDRESS";
$root_pass = "ROOT_CPANEL_PASSWORD!";


$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);

$account = "YOUR_CPANEL_MAIN_ACCNT_NAME";


print $xmlapi->api2_query($account, 'SubDomain','addsubdomain', array(dir=>"public_html/NAME_OF_SUBDOMAIN", domain=>"NAME_OF_SUBDOMAIN", rootdomain=>"MAIN_DOMAIN.com") );
Paralipomena answered 22/3, 2011 at 18:28 Comment(0)
J
0

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
}
Jequirity answered 20/10, 2010 at 9:54 Comment(0)
F
0

This can be achieved in .htaccess provided your server is configured to allow wildcard subdomains. I achieved that in JustHost by creating a subomain manually named * and specifying a folder called subdomains as the document root for wildcard subdomains. Add this to a .htaccess file in your subdomains folder:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]

Finally, create a folder for your subdomain and place the subdomains files.

Faraday answered 6/4, 2016 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.