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

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

    You're looking to create a custom A record.

    I'm pretty sure that you can use wildcards when specifying A records which would let you do something like this:

    *.mywebsite.com       IN  A       127.0.0.1
    

    127.0.0.1 would be the IP address of your webserver. The method of actually adding the record will depend on your host.


    Doing it like http://mywebsite.com/user would be a lot easier to set up if it's an option.

    Then you could just add a .htaccess file that looks like this:

    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteRule ^([aA-zZ])$  dostuff.php?username=$1
    

    In the above, usernames are limited to the characters a-z


    The rewrite rule for grabbing the subdomain would look like this:

    RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
    RewriteRule (.*)  dostuff.php?username=%1
    

提交回复
热议问题