Create subdomains on the fly with .htaccess (PHP)

后端 未结 9 1509
情深已故
情深已故 2020-11-22 02:07

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

相关标签:
9条回答
  • 2020-11-22 02:08

    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

    0 讨论(0)
  • 2020-11-22 02:09

    Wildcard subdomain creation methods

    FIrst you have to create the DNS settings using your server DNS editor.

    1. Create A record in DNS settings with host * wild card in server ip address.

      * 1400 IN A ip_address

    2. Create once again a A record in DNS settings with host @ or domain_name.tld in server ip address. tld means top level domains or the extension of the domains such as .com, .org, etc....

      @ 1400 IN A ip_address or domain_name.tld 1400 IN A ip_address

    3. Create CNAME record like:

      www 1400 IN A domainname.tld

    4. Create the subdomain with * wildcard like *.domain.tld
    5. Create htaccess in your subdomain directory of *.domain.tld and put this code :

      Options +FollowSymLinks 
      RewriteEngine On 
      RewriteBase /
      RewriteRule ^([aA-zZ0-9]+)$ index.php?data=$1
      RewriteCond %{HTTP_HOST} ^([aA-zZ0-9]+)\.([aA-zZ0-9-]+)\.([aA-zZ]+)
      RewriteRule ([aA-zZ0-9]+) index.php?data=%1
      

      Test your first wildcard subdomain like example.domainname.tld

    If you are not interest to pass the data as parameter using the htaccess, you can also get the data by using the following coding:

    define("SUBDOMAIN_PARENT","domainname.tld");   
    class getData
        {
             function __construct()
            {
                $this->data="";
                $http_host=$_SERVER['HTTP_HOST'];
             $subdom_data= str_replace(SUBDOMAIN_PARENT,"",$http_host);
             $expl_subdom_data=explode(".",$subdom_data);
          $expl_subdom_data=array_filter($expl_subdom_data);
    
                if($expl_subdom_data!=NULL)
                {
               $this->data=end($expl_subdom_data);
                }
           }
        }
    $GLOBALS['get_data']=new getData();
    

    and use your global variable at any place like global $get_data.

    echo $get_data->data; //example
    

    (note: This class mainly used for get the exact subdomainname from http_host. because some extra names combined before your subdomain is also applicable like www.example.domainname.tld. This return $_GET['data']='wwww' So my suggestion is to use the $_SERVER['http_host'] for get the exact values instead of using the $_SERVER['query_string'] or passed htaccess parameters in your index page)

    6.Speed up the execution of this wildcard subdomains use N seconds in TTL - DNS SETTINGS.

    7.Check the subdomain after your given ttl time (600 - 10 minutes) like => http://abc.domain.tld

    (note: wildcard subdomains not override the existed subdomains. Because First priority always for your existed subdomains)

    0 讨论(0)
  • 2020-11-22 02:11

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

    0 讨论(0)
  • 2020-11-22 02:24

    I found it easier doing it with PHP. In fact is creating a subdomain within cPanel and create your folder under the desired domain name. As you will do it manually in cPanel but all it's done in milliseconds by a simple PHP function. No click necessary :)

    function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {
    
        //  $buildRequest = "/frontend/x3/subdomain/doaffffdomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain;
    
        $buildRequest = "/frontend/x3/subdomain/doaffffdomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;
    
        $openSocket = fsockopen('localhost',2082);
        if(!$openSocket) {
            return "Socket error";
            exit();
        }
    
        $authString = $cPanelUser . ":" . $cPanelPass;
        $authPass = base64_encode($authString);
        $buildHeaders  = "GET " . $buildRequest ."\r\n";
        $buildHeaders .= "HTTP/1.0\r\n";
        $buildHeaders .= "Host:localhost\r\n";
        $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
        $buildHeaders .= "\r\n";
    
        fputs($openSocket, $buildHeaders);
            while(!feof($openSocket)) {
               fgets($openSocket,128);
            }
        fclose($openSocket);
    
        $newDomain = "http://" . $subDomain . "." . $rootDomain . "/";
    
       //  return "Created subdomain $newDomain";
    
    }
    
    • See more at: http://vikku.info/programming/php/create-subdomain-dynamically-in-php-code-to-create-subdomains-in-server-using-php.htm#sthash.HV4D2Uig.dpuf
    0 讨论(0)
  • 2020-11-22 02:26

    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

    0 讨论(0)
  • 2020-11-22 02:30

    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.

    0 讨论(0)
提交回复
热议问题