title: Apache Rewrite: image directory based on HTTP host
body: <p>My software supports multiple domain names all pointed at the same directory on the server (a different database for each of course). So these domains...</p>
  
  <pre><code>www.example1.com
  www.example2.com
  www.example3.com
  </code></pre>
  
  <p>...all point to...</p>
  
  <pre><code>/public_html/
  </code></pre>
  
  <p>In the image directory...</p>
  
  <pre><code>/public_html/images/
  </code></pre>
  
  <p>I have directories that exactly match the host names for each website:</p>
  
  <pre><code>/public_html/images/www.example1.com/
  /public_html/images/www.example2.com/
  /public_html/images/www.example3.com/
  </code></pre>
  
  <p>I'm trying to get Apache to rewrite requests so that if you view the image directly and look at the address bar you only see the host name once.</p>
  
  <p>So a request for...</p>
  
  <pre><code>http://www.example1.com/images/book.png
  </code></pre>
  
  <p>...is fetched by Apache at...</p>
  
  <pre><code>/public_html/images/www.example1.com/book.png
  </code></pre>
  
  <p>One of the things I've tried and have had success with in different circumstances is the following though it doesn't work in this situation:</p>
  
  <pre><code>RewriteRule ^[^/]*/images(.+) images/%{HTTP_HOST}/$1
  </code></pre>