-1

How can I create subdomains in PHP? I want every user that signs up on my site to instantly have their own subdomain.

EDIT

I have this working now. First I allowed wildcard subdomains in my server. I achieved that in JustHost by creating a subomain manually named * and specifying a folder called subdomains as the document root for wildcard subdomains. I added this to my .htaccess file in my 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, I created a folder for my subdomain and place the subdomains files.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
  • 1
    This could be what you are looking for http://stackoverflow.com/questions/183928/how-to-let-php-to-create-subdomain-automatically-for-each-user – Kobbe Mar 01 '16 at 02:12

1 Answers1

1

This question has no connection to PHP. It's related to DNS and systems administration.

In very short, there are three steps:

1) Configure DNS so that {subdomain}.domain points to your application. Two approaches - either add a new DNS entry every time a new user signs up, or add a wildcard '*' entry just once so anything before .domain points to the same IP address.

2) Ensure your web server software will accept request for {subdomain}.domain and pass it to your application. Easiest thing - a default virtual host (if no other website matches, send the request to your application so all subdomains will be covered automatically)

3) Do the routing in your PHP application. Check current request address (it's in $_SERVER global variable), see if this subdomain (user) actually exists and then display the website accordingly.

Denis Mysenko
  • 6,366
  • 1
  • 24
  • 33