1

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, I'd love to hear it.

Thanks!

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Joe
  • 111
  • 3

1 Answers1

2

I was having similar problems, with a slightly different pattern, using a '.dev' suffix for my virtual hosts, with the full domain as the folder name.

I got that working with VirtualDocumentRoot /www/sites/%-2+/public_html ...

<VirtualHost *:80>
  ServerName dev
  ServerAlias *.dev
  UseCanonicalName Off
  VirtualDocumentRoot /www/sites/%-2+/public_html
</VirtualHost>

E.g. The above resolves a request for test.com.dev to www/sites/test.com/public_html

I think you just need that last one /www/%2+/public_html/%1 to resolve sub.test.com to www/test.com/public_html/dev but check the docs to find an exact pattern: http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html#interpol

I also found this article pretty helpful: http://blog.theclickpro.com/2010/12/dynamic-apache-configuration-for-php.html

You could also use DNSmasq to make the host entries automatic as well.

Teeks
  • 51
  • 5
  • The most vital parts for me were `ServerName dev` and `ServerAlias *.dev`. Without them, server refused to resolve the virtual hosts at all. – baldrs Jan 26 '15 at 16:02