title: How to write htaccess rewrite for subdirectories recursively?
body: <p>I have an Apache server, hosting a number of websites for my company.  I plan to use htaccess, and rewrite the URLs from the "root" directory to a subfolder.</p>
  
  <p>Here is my real folder structure:</p>
  
  <ul>
  <li>/www (root)</li>
  <li>/www/beamstyle (beamstyle.com.hk, CodeIgniter framework)</li>
  <li>/www/beamcard (beamcard.com.hk, Static files)</li>
  <li>/www/beamcard/app (beamcard.com.hk/app, CodeIgniter framework)</li>
  </ul>
  
  <p>====================================================================</p>
  
  <p>The beamstyle website works using the following code:</p>
  
  <pre><code>ReWriteCond %{HTTP_HOST} ^(www.)?beamstyle.com.hk [NC]
  RewriteCond %{REQUEST_URI} !^/beamstyle/.*$
  RewriteRule ^(.*)$ /beamstyle/$1 [L]
  </code></pre>
  
  <p>The above works, because my framework (CodeIgniter) is inside /www/beamstyle.</p>
  
  <p>Therefore, I can access <a href="http://beamstyle.com.hk" rel="nofollow">http://beamstyle.com.hk</a>, and it will get redirected without any issues.</p>
  
  <p>====================================================================</p>
  
  <p>However, here is the problem.  When I do the beamcard website, it doesn't work because actually, the directory "/www/beamcard/" actually stores ONLY static .html files.  My CodeIgniter framework is inside app/.</p>
  
  <p>When I use the same code:</p>
  
  <pre><code>ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
  RewriteCond %{REQUEST_URI} !^/beamcard/.*$
  RewriteRule ^(.*)$ /beamcard/$1 [L]
  </code></pre>
  
  <p>Here are the results using the above code:</p>
  
  <p>(a) <a href="http://beamcard.com.hk/" rel="nofollow">http://beamcard.com.hk/</a> &lt;-- OK no problem, because this is the immediate folder after rewrite (and contain static files only)</p>
  
  <p>(b) <a href="http://beamcard.com.hk/app" rel="nofollow">http://beamcard.com.hk/app</a> &lt;-- NOT OK, because it steps one directory beyond the immediate folder after rewrite.</p>
  
  <ul>
  <li>If I type this, the location bar (on the top) gets un-disguised and redirected to <a href="http://beamcard.com.hk/beamcard/app/" rel="nofollow">http://beamcard.com.hk/beamcard/app/</a> (I confirmed that this redirect not done by Codeigniter files because the same result happens when I apply on an empty directory)</li>
  </ul>
  
  <p>====================================================================</p>
  
  <p>I've tried everything I could, and did so many Google searches, but failed to fine an htaccess snippet that works on subdirectories BEYOND the directory after rewrite.  It loses it's "disguise" functionality when I step further in the subdirectories.</p>
  
  <p>Any help on this is greatly appreciated!</p>
  
  <p>Cheers,
  Thomas</p>
  
  <p>====================================================================</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>In order to describe my situation better, I've put together some use cases.</p>
  
  <p>[ Case Scenarios ]</p>
  
  <p>** To simplify things, let's assume the "app" directory is empty **</p>
  
  <p>(1) If I type in <a href="http://beamcard.com.hk" rel="nofollow">http://beamcard.com.hk</a>, the page "/www/beamcard/index.html" inside the server should get loaded.  After finish loading the page, the location bar should write "<a href="http://beamcard.com.hk/" rel="nofollow">http://beamcard.com.hk/</a>".</p>
  
  <p>(2) If I type in <a href="http://beamcard.com.hk/contact_us.html" rel="nofollow">http://beamcard.com.hk/contact_us.html</a>, the page "/www/beamcard/contact_us.html"  inside the server should get loaded.  After finish loading the page, the location bar should write "<a href="http://beamcard.com.hk/contact_us.html" rel="nofollow">http://beamcard.com.hk/contact_us.html</a>".</p>
  
  <p>(3) If I type in <a href="http://beamcard.com.hk/app" rel="nofollow">http://beamcard.com.hk/app</a>, an "empty file listing" should be loaded.  After finish loading the page, the location bar should write "<a href="http://beamcard.com.hk/app/" rel="nofollow">http://beamcard.com.hk/app/</a>".</p>
  
  <p>(4) If I type in <a href="http://beamcard.com.hk/app/" rel="nofollow">http://beamcard.com.hk/app/</a>, an "empty file listing" should be loaded.  After finish loading the page, the location bar should write "<a href="http://beamcard.com.hk/app/" rel="nofollow">http://beamcard.com.hk/app/</a>".</p>
  
  <p>========================================================================</p>
  
  <p>Currently, (1) and (2) works.</p>
  
  <p>However, for (3) and (4), after finish loading the page, the location bar got redirected to "<a href="http://beamcard.com.hk/beamcard/app/" rel="nofollow">http://beamcard.com.hk/beamcard/app/</a>", which "reveals" the "/beamcard/" portion, which ideally should be hidden from the site visitor.</p>