I discovered this the hard way, but when setting up virtual hosts in Apache 2.2.x and the virtually hosted site performs URL rewrite using the “mod_rewrite” Apache module, which is required for any MVC application or any site utilizing .htaccess rewrite conditions and rules. According to Apache’s documentation, it is recommended to always use virtual hosts, even if a web server is only hosting 1 site. It does make the setup and configuration more portable for the future.
Anyway, the scenario was I needed to setup up a virtual host on a server, which would reside in a subdirectory of the main web root. I had previously set up a site in the main web root directory, which did not use .htaccess files, although the “mod_rewrite” Apache module was installed and enabled. I followed Apache’s documentation about setting up a virtual host config file for the default site, which is the site in the web root. In the “vhosts.conf”, virtual host config file, I set the <Directory> tag to point to the web root, “/srv/www/htdocs” in my case. I also set the “Options” directive to “Indexes FollowSymLinks”, (FollowSymLinks is required for URL rewriting). Then I also set the “AllowOverride” directive to “All”, which is also required for using any functions in an .htaccess file.
According to Apache’s documentation, the <Directory> settings affect the current directory as well as all of the subdirectories. So I continued to setup a 2nd “<VirtualHost>” tag for the MVC site I uploaded to a subdirectory from the web root. I set the “<Directory>” tag to point to the proper local subdirectory, “/srv/www/htdocs/subdirectory”. I assumed the <Directory> options would trickle down and my MVC site would function properly. (Of course not, that would be too easy!) Anyway, after hours of scouring Google and coming up with nothing but copies of the same tutorials, I tried adding “<Directory>” tag and local directives for “Options” and “AllowOverride” for the 2nd <VirtualHost> settings and it finally worked.
I’m sure system admins around the world know this, but I’m not a system admin, but have to do these tasks, because no one else will do them. Anyway, for any others, including myself, when setting up <VirtualHost> in Apache, each virtually hosted site must have its own local settings and directives, even though, the sites are sitting in subdirectories and should inherit the settings for the parent directory, but that is not the case with virtual hosted sites.