4

I'm using a PHP script that creates a subfolder for each user when he sign up. e.g : domain.com/users/user1

I need to map subdomains to these subfolders e.g : user1.domain.com.

I'm using XAMPP and I have followed this tutorial and it worked well.

BUT I've to do this for every user/subdomain !

I need to do this automatically when user sign up.

Dr ZIZO
  • 333
  • 4
  • 17

4 Answers4

4

You want to do mass virtual hosting on Apache. Here you will find the information how to do that:

Dynamically configured mass virtual hosting

Based on the example from the tutorial you've linked:

NameVirtualHost *
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
  </VirtualHost>
  <VirtualHost *>
    DocumentRoot "C:\Documents and Settings\Me\My Documents\clientA\website"
    ServerName clientA.local
  <Directory "C:\Documents and Settings\Me\My Documents\clientA\website">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
<VirtualHost *>
    DocumentRoot "C:\Documents and Settings\Me\My Documents\clientB\website"
    ServerName clientB.local
  <Directory "C:\Documents and Settings\Me\My Documents\clientB\website">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

The problem concerning this configuration is, that it's static and you need to restart Apache when you change it.

At first, you need an record in your DNS server to map all subdomains to your server. Like this:

*.local. 3600 IN A x.x.x.x

To test this on localhost, you can set some subdomains in your hosts file manually. See here why it is not possible to set a wildcard subdomain in the hosts file.

Don't forget to load the vhost_alias_module in httpd.conf:

LoadModule vhost_alias_module modules/mod_vhost_alias.so

Then you will replace you vhost configuration, like this example:

<VirtualHost *>
    # get the server name from the Host: header
    UseCanonicalName Off

    # this log format can be split per-virtual-host based on the first field
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    CustomLog logs/access_log vcommon

    # include the server name in the filenames used to satisfy requests
    VirtualDocumentRoot "C:/Documents and Settings/Me/My Documents/%1/website"

    <Directory "C:/Documents and Settings/Me/My Documents/*/website">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
        DirectoryIndex  index.php index.html index.htm
    </Directory>
</VirtualHost>

This will use a wildcard to set the document root of the subdomain requested.

Community
  • 1
  • 1
LucasF
  • 883
  • 1
  • 6
  • 17
  • I know its potentially a lot of work but but try to inline at least one basic example form the docs into your answer just in case the link changes (hopefully wont happen since its the apache2 docs, but who knows) and so there is something concrete that others in the future can specifically comment on. – prodigitalson Dec 29 '14 at 19:30
  • @LucasF I've read this topic before and I can't fully understand it or apply it on my website. Can you please simplify it here. – Dr ZIZO Dec 31 '14 at 22:29
1

I have implemented something similar to this by making use of the default vhost on my server. Assuming you have your dns setup so that all subdomains point to the appropriate IP address, then they should all resolve to the default vhost. The folder that the default vhost points too needs to have an index.php file that makes use of the subdomain to serve up the appropriate content.

You can replicate this locally with XAMPP by editing your /etc/hosts file and pointing a subdomain in your local dns to your localhost. Set your webroot to where the index.php file is and get the domain name from the $_SERVER vars. From there you can determine the user by the subdomain and show content programatically.

AndrewVT
  • 335
  • 1
  • 8
1

You can install a web hosting control panel like Plesk or Webmin to take on this job for you. You will use a friendly GUI to configure subdomains and all the nitty-gritty configuration will take place in the background. This is what real-world hosting providers use.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

I have been thinking about this for a while but never tested it. I think you should try the same approach as some MVC frameworks like CodeIgniter

Use index.php to route all requests. get $_SERVER["HTTP_HOST"] and parse it to get your sub domain. Now depending on your sub domain load corresponding view (view as in MVC)

AKT
  • 176
  • 2
  • 10