How do you create sub domains using PHP?
- Wild card domains can be used. Sub domains can be created by first creating a sub directory in the /htdocs folder. E.g. /htdocs/mydomain. Then, the host file needs to be modified to define the sub domain. If the sub domains are not configured explicitly, all requests should be thrown to the main domain. Following are the steps:
1. Set the main domain to act as a catch-all domain. Like if the main domain root is /home/admin/xyz.com/htdocs/ then the catch all setup sends all the sub-domain requests to the same directory i.e. /home/admin/xyz.com/htdocs/.
2. Add the following code at the top of the index page right after " "
$domain = $_SERVER['HTTP_HOST'];
$domain_parts = explode('.',$domain);
if (count($domain_parts) == 3 && $domain_parts!= "www")
// make sure a subdomain is called
$user = $domain_parts;
$loc = "http://domain.tld/somepage.ext?varname=$user";
- By adding this code to the index file, the subdomain will be created.