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

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

    First, you need to make sure your server is configured to allow wildcard subdomains. I achieved that in JustHost by creating a subomain manually named *. I also specified 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, all you need to do is create a folder in your subdomains folder, then place the subdomain's files in that directory.

提交回复
热议问题