1

In an Apache VirtualHost section, is it possible to dynamically assign a DocumentRoot based on hostname instead of having one VirtualHost section for each hostname?

I have many hostnames that all share the same wildcard certificate and many other parameters. So it would be much better to have them all inside one VirtualHost section and set the DocumentRoot bases on the hostname.

choice
  • 33
  • 4
  • You can use `Include` statements to share common settings between virtual hosts. This way you can still have separate log files for vhosts, etc. – ypnos May 23 '20 at 09:15
  • @ypnos True. But I prefer a single conf file. – choice May 23 '20 at 09:20

1 Answers1

1

The module mod_vhost_alias offers this functionality. Instead of DocumentRoot, you would define VirtualDocumentRoot where you can embed variables from the request.

Simplest example from linked documentation:

UseCanonicalName    Off
VirtualDocumentRoot "/usr/local/apache/vhosts/%0"

A request for http://www.example.com/directory/file.html will be satisfied by the file /usr/local/apache/vhosts/www.example.com/directory/file.html.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • Yes, but how can I use that to assign different document roots to different hostnames? Say I want the DR to be "/something/" for the hostname "joe.example.com" and "/else/" for "tim.example.com". – choice May 23 '20 at 09:25
  • You might combine it with *mod_rewrite*. – ypnos May 24 '20 at 09:07