15

Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?

This is naming convention I am currently using and would like to continue to use:

host                    directory
127.0.0.1 domain        domain.com
127.0.0.1 sub.domain    domain.com_sub

Then in my vhosts section of the httpd.conf I have:

NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1>
    VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>

<VirtualHost 127.0.0.1>
    VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>

The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.

Ben
  • 1,441
  • 4
  • 18
  • 20
  • 4
    I'm assuming you solved this issue, but I've posted below for completeness, since this comes up in google searches. You might want to mark this question as answered? – starmonkey Mar 24 '10 at 07:29

5 Answers5

14

Apache typically will pick the first virtual host whose ServerName or ServerAlias matches the host name provided in the Host HTTP header. In your case, since you have no ServerName directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName and ServerAlias to make the configuration explicit. Try something more like this:

NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
    ServerName domain.com
    ServerAlias www.domain.com
    VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
    ServerName sub.domain.com
    ServerAlias *.domain.com
    VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>

That should use /var/www/domain.com for http://domain.com and /var/www/www.domain.com for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com for http://sub.domain.com, /var/www/blah.domain.com for http://blah.domain.com, and so on.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • This saved my day. I was switching between two VirtualHost files with VirtualDocumentRoot definitions. I knew there had to be a better way and you figured it out. Thanks! – Robin van Baalen Feb 05 '14 at 16:02
  • The solution solved my problem. But I have a new question, for some special sub domains, I need do some special config, such as add JK config, or set a customized log file. How can we do that? – Stony Aug 24 '15 at 03:34
  • @Stony I suggest asking on [webmasters.SE] or perhaps [SF]. – David Z Aug 24 '15 at 03:40
4

You have to qualify the backreferences when you want to put a '.' in the file path. So you need to have it like this:

VirtualDocumentRoot /var/www/%2.0.%3_%1
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
cha0s
  • 53
  • 3
2

Regarding the OP and the issue with "/var/www/html" being set:

The problem I had to this was using %1 instead of %2. Here's my working example:

ServerAlias www.*.org.au
UseCanonicalName Off
VirtualDocumentRoot /path/to/sites/%2/pub

Hope that helps someone!

I read the docs on "Directory Name Interpolation" in mod_vhost_alias docs.

starmonkey
  • 3,147
  • 2
  • 20
  • 15
1

I finally found a configuration that allows flexible subdomain creation.

See apache docs on mod_vhost_alias

If your root dev domain has 3 parts like dev.example.com you can use %-4+ as a placeholder for everything before the root domain. If it has 4 parts, use %-5+.

<VirtualHost *:80>
    VirtualDocumentRoot "/var/www/%-4+/webroot"
    ServerName www.dev.example.com
    ServerAlias *.dev.example.com
    php_admin_value auto_prepend_file /var/www/setdocroot.php
</VirtualHost>

This way you can create a directory named /var/www/sub.domain/webroot and access it with the url sub.domain.dev.example.com.

The line php_admin_value auto_prepend_file /var/www/setdocroot.php fixes the docroot on some systems like OSX 10.9+

Here is the content of setdocroot.php :

<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
?>
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
0

What I am noticing with this configuration is that $_SERVER['DOCUMENT_ROOT'] is pointing to /var/www/html and not to the vhost's doc root. weird.

Update (2010-07-24):

I just wrote a blog post how to setup your http proof server http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html

Svetoslav Marinov
  • 1,498
  • 14
  • 11