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

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

    You could [potentially] do a rewrite of the URL, but yes: you have to have control of your DNS settings so that when a user is added it gets its own subdomain.

    0 讨论(0)
  • 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.

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

    I do it a little different from Mark. I pass the entire domain and grab the subdomain in php.

    RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}
    

    This ignores images and maps everything else to my index.php file. So if I go to

    http://fred.mywebsite.com/album/Dance/now
    

    I get back

    http://fred.mywebsite.com/index.php?uri=album/Dance/now&hostName=fred.mywebsite.com
    

    Then in my index.php code i just explode my username off of the hostName. This gives me nice pretty SEO URLs.

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

    Don't fuss around with .htaccess files when you can use Apache mass virtual hosting.

    From the documentation:

    #include part of the server name in the filenames VirtualDocumentRoot /www/hosts/%2/docs

    In a way it's the reverse of your question: every 'subdomain' is a user. If the user does not exist, you get an 404.

    The only drawback is that the environment variable DOCUMENT_ROOT is not correctly set to the used subdirectory, but the default document_root in de htconfig.

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

    In addition to configuration changes on your WWW server to handle the new subdomain, your code would need to be making changes to your DNS records. So, unless you're running your own BIND (or similar), you'll need to figure out how to access your name server provider's configuration. If they don't offer some sort of API, this might get tricky.

    Update: yes, I would check with your registrar if they're also providing the name server service (as is often the case). I've never explored this option before but I suspect most of the consumer registrars do not. I Googled for GoDaddy APIs and GoDaddy DNS APIs but wasn't able to turn anything up, so I guess the best option would be to check out the online help with your provider, and if that doesn't answer the question, get a hold of their support staff.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题