Create subdomains on the fly with .htaccess (PHP)

后端 未结 9 1508
情深已故
情深已故 2020-11-22 02:07

I am looking to create a system which on signup will create a subdomain on my website for the users account area.

e.g. johndoe.website.com

I think it would

9条回答
  •  忘了有多久
    2020-11-22 02:32

    You could allow every subdomain in the first place and then check if the subdomain is valid. For example:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$
    RewriteRule !^index\.php$ index.php [L]
    

    Inside the index.php you can than extract the subdomain using:

    if (preg_match('/^([^.]+)\.example\.com$/', $_SERVER['HTTP_HOST'], $match)) {
        var_dump($match[1]);
    }
    

    But all this requires that your webserver accepts every subdomain name.

提交回复
热议问题