How to let PHP to create subdomain automatically for each user?

前端 未结 12 992
暖寄归人
暖寄归人 2020-11-22 13:43

How do I create subdomain like http://user.mywebsite.com ? Do i have to access htaccess somehow? Is it actually simply possible to create it via pure php code or I need to u

相关标签:
12条回答
  • 2020-11-22 14:14

    We setup wildcard DNS like they explained above. So the a record is *.yourname.com

    Then all of the subdomains are actually going to the same place, but PHP treats each subdomain as a different account.

    We use the following code:

    $url=$_SERVER["REQUEST_URI"];
    $account=str_replace(".yourdomain.com","",$url);
    

    This code just sets the $account variable the same as the subdomain. You could then retrieve their files and other information based on their account.

    This probably isn't as efficient as the ways they list above, but if you don't have access to BIND and/or limited .htaccess this method should work (as long as your host will setup the wildcard for you).

    We actually use this method to connect to the customers database for a multi-company e-commerce application, but it may work for you as well.

    0 讨论(0)
  • 2020-11-22 14:16

    I just wanted to add, that if you use CloudFlare (free), you can use their API to manage your dns with ease.

    0 讨论(0)
  • 2020-11-22 14:17

    The feature you are after is called Wildcard Subdomains. It allows you not have to setup DNS for each subdomain, and instead use apache rewrites for the redirection. You can find a nice tutorial here, but there are thousands of tutorials out there. Here is the necessary code from that tutorial:

    <VirtualHost 111.22.33.55>
        DocumentRoot /www/subdomain
        ServerName www.domain.tld
        ServerAlias *.domain.tld
    </VirtualHost>
    

    However as it required the use of VirtualHosts it must be set in the server's httpd.conf file, instead of a local .htaccess.

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

    Create Dynamic Subdomains using PHP and Htaccess

    (1) Root .htaccess

    This file is redirection http://www.yourwebsite.com to http://yourwebsite.com for home page use. All of the subdomain redirection to yourwebsite_folder

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www.yourwebsite.com
    RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^yourwebsite\.com $
    RewriteCond %{REQUEST_URI} !^/yourwebsite_folder/
    RewriteRule (.*) /yourwebsite_folder/$1
    
    RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com
    RewriteCond %{REQUEST_URI} !^/yourwebsite_folder/
    RewriteRule (.*) /yourwebsite_folder/$1
    

    (2) Inside Folder .htaccess

    This file is rewriting the subdomain urls.

    http://yourwebsite.com/index.php?siteName=9lessons to http://9lessons.yourwebsite.com

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

    More .htaccess tips: Htaccess File Tutorial and Tips.

    index.php

    This file contains simple PHP code, using regular expressions validating the subdomain value.

    <?php
    $siteName='';
    if($_GET['siteName'] )
    {
    $sitePostName=$_GET['siteName'];
    $siteNameCheck = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $sitePostName);
       if($siteNameCheck)
       {
         //Do something. Eg: Connect database and validate the siteName.
       }
       else
      {
        header("Location: http://yourwebsite.com/404.php");
       }
    }
    ?>
    //HTML Code
    <!DOCTYPE html>
    <html>
    <head>
    <title>Project Title</title>
    </head>
    <body>
    <?php if($siteNameCheck) { ?>
    //Home Page
    <?php } else { ?>
    //Redirect to Subdomain Page.
    <?php } ?>
    </body>
    </html>
    

    No Subdomain Folder

    If you are using root directory(htdocs/public_html) as a project directory, use this following .htaccess file.

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^www.yourwebsite.com
    RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L]
    
    RewriteRule ^([aA-zZ])$ index.php?siteName=$1
    RewriteCond %{HTTP_HOST} ^(^.*)\.yourwebsite.com
    RewriteRule (.*) index.php?siteName=%1
    
    0 讨论(0)
  • 2020-11-22 14:25

    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 your .htaccess file:

    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.

    0 讨论(0)
  • 2020-11-22 14:26

    Simple PHP solution for subdomains and multi-domain web apps

    Step 1. Provide DNS A record as "*" for domains (or domain) you gonna serve "example.org"

    A record => *.example.org
    A record => *.example.net
    

    Step 2. Check uniquity of logins when user registering or changing login. Also, avoid dots in those logins.

    Step 3. Then check the query

            // Request was http://qwerty.example.org
            $q = explode('.', $_SERVER['HTTP_HOST']);
            /*
                We get following array
                Array
                (
                    [0] => qwerty
                    [1] => example
                    [2] => org
                )
            */
    
            // Step 4.
            // If second piece of array exists, request was for 
            // SUBDOMAIN which is stored in zero-piece $q[0]
            // otherwise it was for DOMAIN
    
            if(isset($q[2])) {
                // Find stuff in database for login $q[0] or here it is "qwerty"
                // Use $q[1] to check which domain is asked if u serve multiple domains
            }
    
    ?>
    

    This solution may serve different domains

    qwerty.example.org
    qwerty.example.net 
    
    johnsmith.somecompany.com
    paulsmith.somecompany.com
    

    If you need same nicks on different domains served differently, you may need to store user choise for domain when registering login.

    smith.example.org // Show info about John Smith
    smith.example.net // Show info about Paul Smith 
    
    0 讨论(0)
提交回复
热议问题