How to automatically map a subdomain to a subfolder

I am using a PHP script that creates a subfolder for each user on registration. for example: domain.com/users/user1

I need to map subdomains to these subfolders, for example: user1.domain.com.

I am using XAMPP and I followed this tutorial and it worked well.

BUT I have to do this for every user / subdomain!

I need to do this automatically when the user signs up.

+3


source to share


4 answers


You want to do bulk shared hosting on Apache. Here's how to do it:

Dynamically Configured Mass Shared Hosting

Based on an example from the tutorial, you 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 with this configuration is that it is static and you need to restart Apache when it changes.

First you need a record on 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 to your file hosts

manually. See here why it is not possible to set a substitution subdomain in a file hosts

.

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

LoadModule vhost_alias_module modules/mod_vhost_alias.so

      

Then you replace the vhost config 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 requested subdomain.

+3


source


I have implemented something similar to this using the default application on my server. Assuming you have your DNS system set up so that all subdomains point to the corresponding IP address, then they should all be allowed by default for vhost. The folder that the vhost point to by default must also have an index.php file that uses the subdomain to serve the relevant content.



You can replicate this locally with XAMPP by editing the / etc / hosts file and pointing the subdomain in local DNS to your localhost. Set your webroot to where your index.php file is located and get the domain name from $ _SERVER vars. From there, you can identify the user by subdomain and display the content programmatically.

+1


source


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

+1


source


I have been thinking about this for a while but have 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 secondary domain. Now, depending on your subdomain, load the appropriate view (like in MVC)

0


source







All Articles