VirtualDocumentRoot Setup For Local Development

I’m trying to learn about setting up httpd.conf and httpd-vhosts.conf files. I would like to have a local setup, using Wampserver on Windows 7, so that I can add a new project just by adding a new directory and a quick edit to the hosts file. I would like for it to work at least for the following general domain formats: domain.com, www.domain.com, sub.domain.com, and www.sub.domain.com.

The following is what I have so far. It seems to work, but I’m just thinking there is likely a better, possibly more succinct way:

NameVirtualHost *:80

<Directory "C:/wamp/www/%0/public_HTML">
    Options Indexes FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    VirtualDocumentRoot "C:/wamp/www/%0/public_HTML"
</VirtualHost>

<VirtualHost *:80>
    ServerAlias www.*.*.com
    VirtualDocumentRoot "C:/wamp/www/%3+/public_HTML/%2"
</VirtualHost>

<VirtualHost *:80>
    ServerAlias www.*.com
    VirtualDocumentRoot "C:/wamp/www/%2+/public_HTML"
</VirtualHost>

<VirtualHost *:80>
    ServerAlias *.*.com
    VirtualDocumentRoot "C:/wamp/www/%2+/public_HTML/%1"
</VirtualHost>

My folder structure is as follows: C:/wamp/www/(project)/public_HTML/(folder named as the sub part of sub.domain.com)

This works, but if I change the order of the VirtualHosts in any way, it causes a 404 error for at least one of the domain formats listed above. I was wondering if anyone could explain to me why that is? And also, if someone has a suggestion for a better way to approach all of this, I’d love to hear it.

Thanks!

jj4,

You’ve not told your server anything about the virtual domains you want to establish!

Okay, that’s probably not your fault as WAMP may not have given you Apache’s httpd-vhosts.conf file with the comments to help you. So, using my test server as an example …


<VirtualHost 127.0.0.1:80>
  ServerName dk
  DocumentRoot W:/dk
  ServerPath W:/dk
</VirtualHost>

Extending that to your selection of virtual host domains, you’d have something like


<VirtualHost 127.0.0.1:80>
  ServerName domain
  DocumentRoot D:/abc
  ServerPath D:/abc
</VirtualHost>

<VirtualHost 127.0.0.1:80>
  ServerName sub.domain
  DocumentRoot D:/xyz
  ServerPath D:/xyz
</VirtualHost>

I believe that Apache will handle the www or non-www versions for you (I’m lazy - I don’t like to type the www. for my test server) but, if not, add a line ( ServerAlias D:/www.domain) with the www’d version of your LOCAL domain. WARNING: Do NOT add the TLD to your local domain or you’ll not be able to get to domain.com on the Internet!

Don’t forget that you will also need a pair of lines in the hosts file like

127.0.0.1 domain
# and
127.0.0.1 sub.domain

FWIW, the ordering of your example code would allow the wildcards you used to capture the same requests but in a different order (as you relocate VirtualHost blocks).

Regards,

DK