Create subdomain upon user registration

后端 未结 4 869
深忆病人
深忆病人 2020-12-15 01:24

I have a website where I want users that sign up to get their own subdomain. This subdomain is virtual, and every subdomain uses the same web server files.

I use PHP

4条回答
  •  悲&欢浪女
    2020-12-15 02:02

    Can you tell apache to read an extra .conf file? (traditionally you store your vhosts in httpd-vhosts.conf)

    if so, add something like the following and restart your webserver

    NameVirtualHost *:80
    
    
            DocumentRoot /abs/path/to/webroot
            ServerName   domainname.com
            ServerAlias *.domainname.com
            
                    AllowOverride All
                    Order allow,deny
                    Allow from all
            
    
    

    then in php, you can see which subdomain the user is requesting by inspecting:

    $_SERVER['HTTP_HOST']
    

    ie. if the user requests http://user1.domainname.com/index.php

    $_SERVER['HTTP_HOST'] will have user1.domainname.com

    you can then explode('.', $_SERVER['HTTP_HOST']) to inspect each segment.. etc.

提交回复
热议问题