title: Using Apache and mod_rewrite to direct traffic based in query string value
body: <p>I am trying, essentially, to load balance two servers using (for reasons outside the scope of this question) the first character of a query string parameter in order to decide which server to send traffic.</p>
  
  <p>To explain what I have so far, if we take "user" as the parameter, and assume there are two servers. Server One for serving users who’s names begin with letter between A and M, and a second server for N to Z.</p>
  
  <p>So for example a request for </p>
  
  <p><a href="https://www.example.com/service/" rel="nofollow">https://www.example.com/service/</a>?<strong><em>user=paul</em></strong></p>
  
  <p>would go to </p>
  
  <p>https://<strong>*a-m.internal.</strong>*example.com/service/?user=paul</p>
  
  <p>And request for </p>
  
  <p><a href="https://www.example.com/service/" rel="nofollow">https://www.example.com/service/</a>?<strong><em>user=orla</em></strong></p>
  
  <p>would go to </p>
  
  <p>https://<strong>*n-z.internal.</strong>*example.com/service/?user=orla</p>
  
  <p>So far I have something like this:</p>
  
  <pre><code>RewriteEngine On
  RewriteCond %{Query_String} ^[\?\&amp;]user=[n-z].*
  RewriteRule ^(.*) https://n-z. internal .example.com/service/$1 [P]
  
  RewriteRule ^(.*) https://a-m.internal.example.com/service/$1 [P,L]
  </code></pre>
  
  <p>Firstly, I just Rewrite if it is n-z, and append the URL to the rule ($1). If it doesn’t match n-z, then I send it to a-m by default.</p>
  
  <p>I am new to mod_rewrite, and I can’t seem to find a combination or permutation that will behave as I want. </p>
  
  <p>Could someone explain why I don’t get the expect result?
  Thanks!</p>