By Pablo Godel
If you run a local Apache web server, you are probably familiar with the directive VirtualHost. It allows you to run multiple independent websites on the same machine. I use it all the time to develop my web apps and create fake hostnames, like example.local or site.local which allow me to access these apps by entering http://example.local/ in the browser.
If you are like me, you probably create these quite often, in fact, every time you start a new project, or download a new piece of software that you want to try. To do this, you need to create a new VirtualHost, add the fake hostname to the hosts file and then restart Apache. This is repetitive and as you will soon find out, quite unnecessary.
I included this tip in my talk “Productive Development Environment” (in Spanish) at 2011 Jornadas Symfony in Castellón, Spain and it was a big hit among the developers, even experienced ones liked it, so I decided to share in our blog.
VirtualDocumentRoot allows you to define multiple VirtualHost sections dynamically with one configuration line and without having to restart Apache when you need a new VirtualHost. Simply include the following lines in your Apache configuration:
UseCanonicalName Off VirtualDocumentRoot /Users/pgodel/Sites/%0/web
Note: this example is specially created for symfony apps which use the web directory as the document root. You can change this to public for Zend Framework apps or others.
Then, when you need a new VirtualHost, simply add example.local to your hosts file and create a directory with the .local extension in the directory name. In my case:
Then enter the http://example.local/ URL in your browser and your are done! I hope you like it and helps you be more productive.