PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 21, 2022

[FIXED] How to map subdomain to subfolder automatically

 September 21, 2022     php, virtualhost, xampp     No comments   

Issue

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.


Solution

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.



Answered By - LucasF
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing