I'm running into a problem using Apache's mod_vhost_alias
directives and mod_rewrite
for routing "pretty URLs" with a local development machine. I know that it's possible to do so, because my hosting provider uses a similar configuration and has no problems. However, whenever I try on my local machine, Apache returns a 500 Internal Server Error, and the logs indicate too many internal redirects.
Normally Done
I typically follow a simple conventional process when adding domains to my local machine for development:
- Create a directory in
/path/to/www/
named according to the domain, ie:/path/to/www/example.local/
- Add an entry into
/etc/hosts
for the new domain, ie:127.0.0.1 example.local
- Add a
VirtualHost
directive for the new domain with theDocumentRoot
set to/path/to/www/example.local/pub/
- Restart Apache
The Problem
I recently started using the VirtualDocumentRoot
directive supplied by mod_vhost_alias
to clean up my ever-growing list of VirtualHost
directives and save me the hassle of restarting Apache after every new domain:
CanonicalName Off
VirtualDocumentRoot /path/to/www/%0/pub/
Within one of the domains, say example.local
, I might have the following:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule .* index.php/%0 [L]
That's a typical "pretty URL" / routing pattern for most modern applications and frameworks. However, Apache enters an infinite redirect loop when I attempt to load example.local/anything/that/should/work
in my browser.
I enabled the RewriteLog
to determine what was going on, and found that Apache was performing the following:
- Remove the directory prefix (
/path/to/www/example.local/pub/
) from the URL - Process the
RewriteRules
normally, as expected, generating/index.php/anything/that/should/work
- Prepend the directory prefix again, yielding:
/path/to/www/example.local/pub/index.php/anything/that/should/work
- Start over. (!?!?)
The Question
Has anyone else seen this behavior and formulated a fix for it? Alternatively, can someone see a glaring error in my Apache config that I might be missing?