Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

In my Apache (Xampp) httpd-vhosts.conf file I have added this Virtualhost It allows me to use Wildcard names like testserver1.dev and testserver2.dev I just have to make sure to add the name to my Windows Hosts file.

<VirtualHost *:80>
  VirtualDocumentRoot  E:/Server/htdocs/projects/%1/www
  ServerAlias *.dev
</VirtualHost>

What I would like to do though is add to this funtionality and make it work if the name begins with a www so testserver1.dev would also work as www.testserver1.dev

The way it currently is set up, if I tried to access that URL, it would look in a folder called www.testserver1 instead of the folder testserver1

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Get rid of the ServerAlias and change the %1 to %-2.

<VirtualHost *:80>
  VirtualDocumentRoot  E:/Server/htdocs/projects/%-2/www
</VirtualHost>

As explained in the Directory Name Interpolation section of the mod_vhost_alias documentation, the %-2 will tell Apache to look at the second from the last part of the name.

The next section in the linked document has examples, one of which is the following:

VirtualDocumentRoot "/usr/local/apache/vhosts/%-2.0.%-1.0"

Which will allow www.example.com as well as www.sub.example.com or example.com to serve files from /usr/local/apache/vhosts/example.com. It does this as follows:

%-2.0  # The second from the last part of the name with a zero-length substring.
       # The .0 is necessary as we need to specify a literal period next and don't
       # want the parser to confuse it with a substring specification.

.      # a literal period

%-1.0  # the last part of the name with a zero-length substring. The .0 should be
       # optional here as there's nothing left to cause confusion, but it doesn't
       # hurt anything to have it.
share|improve this answer
    
Great thanks, I should note I had to keep using ServerAlias otherwise it would just route to my servers root –  jasondavis Sep 26 '12 at 1:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.