title: Routing image requests to separate subdomains
body: <p>First, a bit of background info:</p>
  
  <blockquote>
    <p>The HTTP 1.1 specification, circa 1999, recommends that browsers and servers limit parallel requests to the same hostname to two. (<strong><a href="http://www.websiteoptimization.com/speed/tweak/parallel/" rel="nofollow noreferrer">more</a></strong>)</p>
  </blockquote>
  
  <p>If you carry on reading that article the author suggests "fooling" browsers by having multiuple subdomains all pointing to the same thing.</p>
  
  <p>If I was to serve my images from two separate subdomains (two different hostnames) then the browser would download a maximum of 4 images in parallel (2 per hostname).</p>
  
  <p>Given that, I could now equally spread out the requests between the two subdomains to optimise page download speed, like so:</p>
  
  <pre><code>&lt;img src="http://subdomain1.example.com/img1.jpg" /&gt;
  &lt;img src="http://subdomain2.example.com/img2.jpg" /&gt;
  &lt;img src="http://subdomain1.example.com/img3.jpg" /&gt;
  &lt;img src="http://subdomain2.example.com/img4.jpg" /&gt;
  </code></pre>
  
  <p>This would require me to manually go through the appropriate files and change the 'src' of each image.</p>
  
  <hr>
  
  <p>I'm looking for a far more simple/reusable solution that involves no visible changes to the HTML. </p>
  
  <p>I have an idea:</p>
  
  <ol>
  <li>All image-like URLS on [example.com] are redirected (via .htaccess) to [example.com/imghandler.php]</li>
  <li>imghandler.php redirects to either subdomain1 or subdomain2 - randomly chosen.</li>
  </ol>
  
  <p>To illustrate:</p>
  
  <pre><code># Request from browser:
  &gt;&gt; http://example.com/dir/image.jpg
  
  # Rewritten to:
  &gt;&gt; http://example.com/imghandler.php?location=%2Fdir%2Fimage.jpg
  
  # *Redirects* to either:
      1:
          &gt;&gt; http://subdomain1.example.com/dir/image.jpg
             (this is where the browser ends up getting the image from)
      2:
          &gt;&gt; http://subdomain2.example.com/dir/image.jpg
             (this is where the browser ends up getting the image from)
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li><strong>From a theoretical point of view, would this work?</strong></li>
  <li><strong>Is there a better way of accomplishing what I want?</strong></li>
  </ol>