SuperNinja
SuperNinja

Reputation: 1606

creating subdomains for PHP application

I am trying to use the following scenario to create to give registered users there own subdomain to the site. I do not want to add DNS records for every user but if user jon signs up I would like him to see the URL of jon.website.com. It seems that this is possible from other questions that I have read up on.

However my implementation and lack of understanding is holding me up. Here is what I have done so far.

Added DNS wildcard A record for domain

*.website.com --> IP of website ex. 12.2.3.4

Added the following code to .htaccess file in www.website.com/application/

<VirtualHost *:80>
  ServerName server.website.com
  ServerAlias *.website.com
  UseCanonicalName Off
</VirtualHost>

Find subdomain with PHP

preg_match('/([^.]+)\.example\.org/', $_SERVER['SERVER_NAME'], $matches);
if(isset($matches[1])) {
    $subdomain = $matches[1];
}

How I would see this working is, when the user signs up, a directory with the subdomain name is created as in www.website.com/application/subdomainName/ -- I can handle this no prob -- Then on successful login the user is then directed to www.website.com/application/jon/

Am I headed the right direction on this? If yes then how do I get from where I am I where I need to go? This one is quite a bit above my pay grade! Thanks for any advise!

Upvotes: 0

Views: 284

Answers (1)

JohnP
JohnP

Reputation: 50029

Your starting steps are correct and in the right direction. I don't understand why you are creating the directory though.

You just need to make sure that your site works from any domain. So make sure all URLs and links to resources use an absolute path.

I'm guessing you will want to customize the experience for each user. So, you need to store some meta data against each of your registered users so their sites have a different look and feel. The easiest way to do this is to store the meta data against the subdomain in your database. Simply look at the current subdomain your site is running under and load up the appropriate user to figure out how the site should look.

Upvotes: 1

Related Questions