Note that there are some explanatory texts on larger screens.

plurals
  1. PO500 Server Error: Infinite rewrite loop in Apache 2.2.x with mod_vhost_alias, VirtualDocumentRoot, and mod_rewrite for routing / "pretty" URLs
    primarykey
    data
    text
    <p>I'm running into a problem using Apache's <code>mod_vhost_alias</code> directives and <code>mod_rewrite</code> 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.</p> <h1><em>Normally</em> Done</h1> <p>I typically follow a simple conventional process when adding domains to my local machine for development:</p> <ul> <li>Create a directory in <code>/path/to/www/</code> named according to the domain, ie: <code>/path/to/www/example.local/</code></li> <li>Add an entry into <code>/etc/hosts</code> for the new domain, ie: <code>127.0.0.1 example.local</code></li> <li>Add a <code>VirtualHost</code> directive for the new domain with the <code>DocumentRoot</code> set to <code>/path/to/www/example.local/pub/</code></li> <li>Restart Apache</li> </ul> <h1>The Problem</h1> <p>I recently started using the <code>VirtualDocumentRoot</code> directive supplied by <code>mod_vhost_alias</code> to clean up my ever-growing list of <code>VirtualHost</code> directives and save me the hassle of restarting Apache after every new domain:</p> <pre><code>CanonicalName Off VirtualDocumentRoot /path/to/www/%0/pub/ </code></pre> <p>Within one of the domains, say <code>example.local</code>, I might have the following:</p> <pre><code>RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-l RewriteRule .* index.php/%0 [L] </code></pre> <p>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 <code>example.local/anything/that/should/work</code> in my browser.</p> <p>I enabled the <code>RewriteLog</code> to determine what was going on, and found that Apache was performing the following:</p> <ul> <li>Remove the directory prefix (<code>/path/to/www/example.local/pub/</code>) from the URL</li> <li>Process the <code>RewriteRules</code> normally, as expected, generating <code>/index.php/anything/that/should/work</code></li> <li>Prepend the directory prefix again, yielding: <code>/path/to/www/example.local/pub/index.php/anything/that/should/work</code></li> <li>Start over. (!?!?)</li> </ul> <h1>The Question</h1> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload