Create subdomains on the fly with .htaccess (PHP)

后端 未结 9 1687
情深已故
情深已故 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:26

    The easiest way is to redirect all subdomains (with wildcard *) to point to your /wwwroot. Then put .htaccess to this folder with the following code:

    RewriteCond %{ENV:REDIRECT_SUBDOMAIN} ="" 
    RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.com\.?(:80)?$ [NC] 
    RewriteCond %{DOCUMENT_ROOT}/%1 -d 
    RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L] 
    RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
    

    This will accomplish that every subfolder of the /wwwroot folder in acceptable via subdomain (foldername.domain.com).

    Found this years ago on http://www.webmasterworld.com/apache/3163397.htm

提交回复
热议问题