Search code examples
phpapachexamppvirtual-hostshosts-file

How can I create dynamic virtual hosting like *.example.com?


Where * is different for each client like apple.example.com, pearl.example.com. These all should be routed to the same IP and same directory. How to modify the hosts file and the httpd config?

More detailed example:

<VirtualHost 111.22.33.44>
    ServerName                 customer-1.example.com
    DocumentRoot        "/www/hosts/example.com/docs"
    ScriptAlias  "/cgi-bin/"  "/www/hosts/example.com/cgi-bin"
</VirtualHost>

<VirtualHost 111.22.33.44>
    ServerName                 customer-2.example.com
    DocumentRoot        "/www/hosts/example.com/docs"
    ScriptAlias  "/cgi-bin/"  "/www/hosts/example.com/cgi-bin"
</VirtualHost>

<VirtualHost 111.22.33.44>
    ServerName                 customer-N.example.com
    DocumentRoot        "/www/hosts/example.com/docs"
    ScriptAlias  "/cgi-bin/"  "/www/hosts/example.com/cgi-bin"
</VirtualHost>

Thanks!


Solution

  • You can use star in ServerAlias like this:

    <VirtualHost 111.22.33.44>
        ServerName      customer-1.example.com
        ServerAlias     *.example.com
        DocumentRoot    "/www/hosts/example.com/docs"
        ScriptAlias     "/cgi-bin/"  "/www/hosts/example.com/cgi-bin"
    </VirtualHost>