title: Rewrite Rule with dynamic hostname
body: <p>I'm interesting in rewrite rule to modify some url and paths. Here some Examples.
  I have some hostnames which lookups to 1 VirtualHost throught ServerAlias in Apache conf.
  <br />
  example.com<br />
  anotherdomain.com<br />
  same.org<br />
  <br />
  And a lot of anothers domains. Here, what I want to do:<br /></p>
  
  <pre><code>example.com/uploads/photo.png -&gt; /www/example.com/uploads/example.com/photo.png&lt;br /&gt;
  
  anotherdomain.com/uploads/photo.png -&gt; /www/anotherdomain.com/uploads/anotherdomain.com/photo.png&lt;br /&gt;
  </code></pre>
  
  <p>And same thing with another domains. This thing I've made already. </p>
  
  <pre><code>RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
  RewriteRule ^uploads/([a-zA-Z0-9-\._]+)$ uploads/%2/$1 [L]
  </code></pre>
  
  <p>This work perfectly. But, if I try to access uploads/icons/category/image.png <br />
  I start to see 500 Internal Apache Error...
  I've modify Rule to this:</p>
  
  <pre><code>RewriteCond %{REQUEST_URI} !^/uploads/%{HTTP_HOST}
  RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
  RewriteRule ^uploads/([a-zA-Z0-9-\._/]+)$ uploads/%2/$1 [L]
  </code></pre>
  
  <p>And Enable Apache Rewrite log with 9 verbosity. <br /></p>
  
  <pre><code>RewriteCond: input='/uploads/icons/category/image.png' pattern='!^/uploads/%{HTTP_HOST}' =&gt; matched
  rewrite 'uploads/icons/category/image.png' -&gt; 'uploads/example.com/icons/category/image.png'
  RewriteCond: input='/uploads/example.com/icons/category/image.png' pattern='!^/uploads/%{HTTP_HOST}' =&gt; matched
  rewrite 'uploads/example.com/icons/category/image.png' -&gt; 'uploads/example.com/example.com/icons/category/image.png'
  </code></pre>
  
  <p>And after that, it's exhausts 10 lookups =(
  <br />
  When I modify to this</p>
  
  <pre><code>RewriteCond %{REQUEST_URI} !^/uploads/example.com
  RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
  RewriteRule ^uploads/([a-zA-Z0-9-\._/]+)$ uploads/%2/$1 [L]
  </code></pre>
  
  <p>It's works perfectle for ALL stuff, but only for 1 domain: example.com
  <br />
  I found another solution, but it's not a perfect.</p>
  
  <pre><code>RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
  RewriteRule ^uploads/([a-zA-Z0-9-\._/]+)$ uploads/%2/$1 [L]
  </code></pre>
  
  <p>This Rule works only if I have existed dir and file. But, if i try to access:<br />
  <code>/uploads/images/not-existed-404.nothing</code> -> It return 500, because this file doesn't exists and rewriting into:<br /></p>
  
  <pre><code>/uploads/example.com/example.com/example.com/example.com.......
  </code></pre>
  
  <p><em>P.S. Sorry for my bad English ;)</em></p>