How to write htaccess rewrite for subdirectories r

2019-09-13 21:22发布

问题:

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.

Here is my real folder structure:

  • /www (root)
  • /www/beamstyle (beamstyle.com.hk, CodeIgniter framework)
  • /www/beamcard (beamcard.com.hk, Static files)
  • /www/beamcard/app (beamcard.com.hk/app, CodeIgniter framework)

====================================================================

The beamstyle website works using the following code:

ReWriteCond %{HTTP_HOST} ^(www.)?beamstyle.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamstyle/.*$
RewriteRule ^(.*)$ /beamstyle/$1 [L]

The above works, because my framework (CodeIgniter) is inside /www/beamstyle.

Therefore, I can access http://beamstyle.com.hk, and it will get redirected without any issues.

====================================================================

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/.

When I use the same code:

ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamcard/.*$
RewriteRule ^(.*)$ /beamcard/$1 [L]

Here are the results using the above code:

(a) http://beamcard.com.hk/ <-- OK no problem, because this is the immediate folder after rewrite (and contain static files only)

(b) http://beamcard.com.hk/app <-- NOT OK, because it steps one directory beyond the immediate folder after rewrite.

  • If I type this, the location bar (on the top) gets un-disguised and redirected to http://beamcard.com.hk/beamcard/app/ (I confirmed that this redirect not done by Codeigniter files because the same result happens when I apply on an empty directory)

====================================================================

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.

Any help on this is greatly appreciated!

Cheers, Thomas

====================================================================

Update 1

In order to describe my situation better, I've put together some use cases.

[ Case Scenarios ]

** To simplify things, let's assume the "app" directory is empty **

(1) If I type in http://beamcard.com.hk, the page "/www/beamcard/index.html" inside the server should get loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/".

(2) If I type in http://beamcard.com.hk/contact_us.html, the page "/www/beamcard/contact_us.html" inside the server should get loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/contact_us.html".

(3) If I type in http://beamcard.com.hk/app, an "empty file listing" should be loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/app/".

(4) If I type in http://beamcard.com.hk/app/, an "empty file listing" should be loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/app/".

========================================================================

Currently, (1) and (2) works.

However, for (3) and (4), after finish loading the page, the location bar got redirected to "http://beamcard.com.hk/beamcard/app/", which "reveals" the "/beamcard/" portion, which ideally should be hidden from the site visitor.

回答1:

Try adding NS at the end (to prevent the rule from being applied to sub-requests):

ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamcard/.*$
RewriteRule ^(.*)$ /beamcard/$1 [L,NS]