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

前端 未结 12 1180
暖寄归人
暖寄归人 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: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.

    
    //HTML Code
    
    
    
    Project Title
    
    
    
    //Home Page
    
    //Redirect to Subdomain Page.
    
    
    
    

    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
    

提交回复
热议问题