title: htaccess dynamic subdomain rewriting
body: <p>I promise I have been looking for hours for the answer here, but it's still not working. Here's what I have:</p>
  
  <pre><code>RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com [NC]
  RewriteCond %{REQUEST_URI} !^/%1/ [NC]
  RewriteRule ^(.*)$ /%1/$1 [L]
  </code></pre>
  
  <p>This creates an infinite loop because line two is not matching correctly. However, if I replace <code>%1</code> in line two with <code>www</code> and go to <code>www.example.com</code>, it works fine. Obviously it no longer works for the other subdomains, but the fact that when I explicitly enter <code>www</code> in line two instead of letting <code>%1</code> take care of it is strange, because I know the value of <code>%1</code> is <code>www</code>.</p>
  
  <p>What I'm trying to accomplish is as follows:
  I have a domain "example.com". In the root directory there are several subdirectories, let's say "dev", "test" and "www". Higher up in my htaccess file I have a rule that forces the domain to redirect to "www.example.com" if the subdomain is empty (example.com), w (w.example.com), or ww (ww.example.com), anything else is left alone (dev.example.com, test.example.com). Now this part of the file is supposed to rewrite the request to the correct subdirectory based on the subdomain. So, a request like "dev.example.com/something.jpg" gets rewritten (not redirected) to "dev.example.com/dev/something.jpg". </p>
  
  <p>The plan here is to be able to create a development and testing environments on the server, without having to recreate an environment on my home server, making sure all the server settings are exactly the same. Also, it could do anything else I wanted it to do I suppose, but that's the immediate need.</p>