如何编写的htaccess重写为子目录递归?递归、子目录、重写、htaccess

2023-09-02 10:05:22 作者:青橘白衫

我有一个Apache服务器,托管一些网站为我公司。我打算使用htaccess的,并重写的URL从根目录下的子目录。

下面是我真正的文件夹结构:

/ WWW(根) / WWW / beamstyle(beamstyle.com.hk,codeIgniter框架) / WWW / beamcard(beamcard.com.hk,静态文件) / WWW / beamcard /应用程序(beamcard.com.hk/app,codeIgniter框架)

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

该beamstyle网站作品使用以下code:

 的RewriteCond%{HTTP_HOST} ^(WWW)?beamstyle.com.hk [NC]
的RewriteCond%{REQUEST_URI}!^ / beamstyle /.*$
重写规则^(。*)$ / beamstyle / $ 1 [L]
 

以上作品,因为我的框架(codeIgniter)是内/ WWW / beamstyle。

所以,我可以访问 http://beamstyle.com.hk ,它会得到不任何重定向的问题。

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

然而,这里的问题。当我做了beamcard的网站,这是行不通的,因为实际上,在目录/ WWW / beamcard /实际上只存储静态HTML文件。我的codeIgniter框架应用程序/. 关于java的getResource方法

当我用同样的code:

 的RewriteCond%{HTTP_HOST} ^(WWW)?beamcard.com.hk [NC]
的RewriteCond%{REQUEST_URI}!^ / beamcard /.*$
重写规则^(。*)$ / beamcard / $ 1 [L]
 

下面是使用上述code的结果:

(一) http://beamcard.com.hk/ < - 确定没问题,因为这是重写后立即文件夹(包含静态文件只)

(二) http://beamcard.com.hk/app < - 不OK ,因为它的步骤超越眼前的文件夹,一个目录后重写。

如果我键入此,位置栏(顶部)被un-伪装,并重定向到 HTTP ://beamcard.com.hk/beamcard/app/ (我确认这不是重定向由codeigniter文件,这样做,因为当我申请上的空目录相同的结果发生)

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

我试着尽我所能,并没有那么多的谷歌搜索,但没有罚款的htaccess的片段改写后的子目录的作品超越了目录。它失去了它的伪装的功能,当我在子目录中进一步加强。

任何帮助是极大的AP preciated!

干杯, 托马斯

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

更新1

为了来形容我的情况好,我收集了一些用例。

[案例情景]

**为了简化起见,我们假设应用程序目录是空的**

(1)如果我输入 http://beamcard.com.hk ,在网页/ WWW /beamcard/index.html在服务器内部应该得到加载。完成后,载入页面,在地址栏中应写 http://beamcard.com.hk/ 。

(2)如果我输入 http://beamcard.com.hk/contact_us.html,在网页/www/beamcard/contact_us.html在服务器内部应该得到加载。完成加载页面后,在地址栏应该写 http://beamcard.com.hk/contact_us.html 。

(3)如果我输入 http://beamcard.com.hk/app ,一空文件清单应加载。完成加载页面后,在地址栏应该写 http://beamcard.com.hk/app/

(4)如果我输入 http://beamcard.com.hk/app/ ,空文件,列出应加载。完成加载页面后,在地址栏应该写 http://beamcard.com.hk/app/

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

目前,(1)和(2)的工作原理。

不过,对于(3)和(4),完成加载该页面后,在地址栏得到重定向到的 http://beamcard.com.hk/beamcard/app/ ,其中揭示中的/ beamcard /部分,在理想情况下应该被隐藏的网站访问者。

解决方案

尝试从应用到子加入 NS 末(以prevent规则请求):

 的RewriteCond%{HTTP_HOST} ^(WWW)?beamcard.com.hk [NC]
的RewriteCond%{REQUEST_URI}!^ / beamcard /.*$
重写规则^(。*)$ / beamcard / $ 1 [L,NS]
 

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.

解决方案

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]