我如何获得的[L]标志的重写规则(的.htaccess)真正的工作? [英] How do I get the [L] flag of RewriteRule (.htaccess) really working?

查看:228
本文介绍了我如何获得的[L]标志的重写规则(的.htaccess)真正的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用于新人:尝试COM prehensively描述我的问题,那句我的问题我公司生产的文本巨大ammount的。如果你不想看整个事情,<击>我的有关意见(读证明) [L] 标志不能正常工作的误解,从它所有的弹簧,位于的补充意见部分。为什么我误解了明显的行为描述我的答案,以及解决方案,以给定的问题。

To newcomers: While trying to comprehensively describe my problem and phrase my questions I produced huge ammount of text. If you don't want to read the whole thing, my observations about (read "proof of") [L] flag not working the misconception, from which it all sprung, is located in Additional observations section. Why I misunderstood apparent behaviour is described in my Answer as well as solution to given problem.

我已经按照我的.htaccess文件code:

I have following code in my .htaccess file:

# disallow directory indexing
Options -Indexes

# turn mod_rewrite on
Options +FollowSymlinks
RewriteEngine on

# allow access to robots file
RewriteRule ^robots.txt$ robots.txt [NC,L]

# mangle core request handler address
RewriteRule ^core/(\?.+)?$ core/handleCoreRequest.php$1 [NC,L]

# mangle web file adresses (move them to application root folder)
# application root folder serves as application GUI address
RewriteRule ^$ web/index.html [L]
# allow access to images
RewriteRule ^(images/.+\.(ico|png|bmp|jpg|gif))$ web/$1 [NC,L]
# allow access to stylesheets
RewriteRule ^(css/.+\.css)$ web/$1 [NC,L]
# allow access to javascript
RewriteRule ^(js/.+\.js)$ web/$1 [NC,L]
# allow access to library scripts, styles and images
RewriteRule ^(lib/js/.+\.js)$ web/$1 [NC,L]
RewriteRule ^(lib/css/.+\.css)$ web/$1 [NC,L]
RewriteRule ^(lib/(.+/)?images/.+\.(ico|png|bmp|jpg|gif))$ web/$1 [NC,L]

# redirect all other requests to application address
# RewriteRule ^(.*)$ /foo/ [R]

我的Web应用程序(和它的.htaccess文件)位于 DOCUMENT_ROOT 子文件夹(从浏览器进行访问为的http://本地主机/富/ )。它有PHP的核心部分,位于富/芯和JavaScript的图形用户界面部分,位于富/网络。由于可以从code以上可以看出,我想只允许访问处理的图形用户界面的所有请求,并以安全的网页文件和重定向的所有其他要求基本应用程序的地址(最新评论指令),单核心的脚本。

My web application (and its .htaccess file) is located in foo subfolder of DOCUMENT_ROOT (accessed from browser as http://localhost/foo/). It has PHP core part located in foo/core and JavaScript GUI part located in foo/web. As can be seen from the code above, I want to allow access only to single core script that handles all requests from GUI and to 'safe' web files and redirect all other requests to base application address (last commented directive).


它的工作,直到我试图通过取消最后一个重定向指令的最后一部分。如果我评论一些线路,适当的页面部分停止工作,等等。

It works until I try the last part by uncommenting the last redirecting directive. If I comment some more lines, the appropriate page parts stop working, etc.

然而,当我去掉最后一行,所有previous规则匹配失败(至少这是我的理解)时,应只执行,页面进入重定向循环(火狐抛出错误页面的东西,如这网页没有正确重定向),因为它重定向到的http://本地主机/富/ 一遍又一遍,永远

However, when I uncomment last line, which should be performed only when matching of all previous rules fails (at least that's what I understand), page goes into redirection cycle (Firefox throws error page with something like "This page isn't redirecting properly"), because it's redirecting to http://localhost/foo/ again and again and again, forever.

我不明白的是这种处理此规则的:

What I don't understand is this processing of this rule:

重写规则^ $网/ index.html的[L]

具体的 [L] 标志。 标志显然不为我工作。:当最后一行被注释,它正确地重定向,但是当我取消它,它总是处理,即使改写应停止[L]标志。任何人有什么想法?

specifically the [L] flag. The flag apparently doesn't work for me. When the last line is commented, it correctly redirects, but when I uncomment it, it is always processed, even though rewriting should stop on [L] flag. Anyone got any ideas?

此外,在阿里纳斯,我会很高兴知道为什么我下尝试修复它也不行:

Also, on a sidenote, I'd be thrilled to know why my following attempt at fixing it doesn't work either:

RewriteEngine on
RewriteRule ^core/(\?.+)?$ core/handleCoreRequest.php$1 [NC,L]
RewriteRule ^(.*)$ web/$1 [L]
RewriteRule ^.*$ /foo/ [L]

这其实并不在所有的工作。就算我取出最后一行,它仍然无法正常重定向任何东西。如何在第一例中的变向工作,如果它不在第二工作?

This actually doesn't work at all. Even if I remove the last line, it still doesn't redirect anything correctly. How does the redirecting work in the first example, if it doesn't work in the second?

这也将是大有裨益给我,如果任何人知道任何方式实际调试这些指令。我花小时,甚至没有丝毫的线索,什么可能是错误的。

It would also be of great benefit to me, if anybody knew any way to actually debug these directives. I spend hours on this without even the slightest clue what could possibly be wrong.


在试图通过bbadour给出的建议(不,我还没有尝试过,但现在我有第二个意见,我给它一个镜头),并没有工作,我已经出来了下面观察。通过重写最后一行是:

After trying the advice given by bbadour (not that I haven't tried it before, but now that I had a second opinion, I gave it another shot) and it didn't work, I've come up with the following observation. By rewriting last line to this:

RewriteRule ^(.*)$ /foo/?uri=$1 [R,L]

或本

RewriteRule ^(.*)$ /foo/?uri=%{REQUEST_URI} [R,L]

和使用萤火虫的网板,我发现了更多的证据,即[L]标志,显然是不能按预期在pviously提到重写规则^ $网/ index.html的[L的$ P $ ] 规则(我们称之为从现在开始规则)。在第一种情况下,我得到 [...] URI =网站/ index.html的在第二种情况下 [...] URI = /富/网站/ index.html的。这意味着,该规则被执行(重写^ $到网/ index.html的),但重写并不止于此。任何更多的想法,请?

and using Firebug's Net panel, I found out more evidence, that the [L] flag is clearly not working as expected in the previously mentioned RewriteRule ^$ web/index.html [L] rule (let's call it THE RULE from now on). In first case I get [...]uri=web/index.html, in second case [...]uri=/foo/web/index.html. That means that THE RULE gets executed (rewrites ^$ to web/index.html), but the rewriting doesn't stop there. Any more ideas, please?

推荐答案

经过搜索和测试的时间,我终于找到了真正的问题和解决方案。希望这将有助于其他人也一样,当他们遇到了同样的问题。

After hours of searching and testing, I finally found the real problem and solution. Hopefully this will help somebody else too, when they come across the same problem.

.htaccess文件处理(即使没有[R]标志)

这意味着当重写规则^ $网/ index.html的[L] 处理,mod_rewrite的正确停止重写,转到文件的末尾,正确重定向以 /foo/web/index.html ,然后服务器开始处理.htaccess文件的新位置,这是相同的文件。现在只有最后一个重写规则比赛和重定向回 /富/ (此时用[R],所以重定向可以在浏览器中观察到的)......和。 htaccess文件再次处理,并再次,又一次......

which means that after the RewriteRule ^$ web/index.html [L] is processed, mod_rewrite correctly stops rewriting, goes to the end of the file, redirects correctly to /foo/web/index.html, and then the server starts processing .htaccess file for the new location, which is the same file. Now only the last rewrite rule matches and redirects back to /foo/ (this time with [R], so the redirect can be observed in browser) ... and the .htaccess file is processed again, and again, and again...

在多为清晰:因为只有硬重定向可以观察到,这似乎是[L]标志将被忽略,但它并非如此。相反,htaccess的被处理两次重定向之间来回 /富/ /foo/web/index.html

Once more for clarity: Because only the hard redirects can be observed, it seems like the [L] flag is ignored, but it is not so. Instead, the .htaccess is processed two times redirecting back and forth between /foo/ and /foo/web/index.html.


要移动虚拟子目录的应用程序根目录下,更多的复杂的条件重写必须使用。变量 THE_REQUEST 是软硬重定向区分有用的:

To virtually move subdirectory to application root directory, additional complex conditional rewrites must be used. Variable THE_REQUEST is useful for distinguishing between hard and soft redirects:

RewriteCond %{THE_REQUEST} ^GET\ /foo/web/
RewriteRule ^web/(.*) /foo/$1 [L,R]

对于要匹配这个重写规则,两个条件必须适用。首先,在第二行,本地URI必须以网​​络/ (相当于具有绝对的Web URI /富/网络/ )。其次,在第一线,真正的请求的URI必须以 /富/网络/ 太。总之,这意味着,当网​​络/ 子文件夹内的文件将直接从浏览器请求,在这种情况下,我们希望做一个硬重定向规则只匹配。

For this rewrite rule to be matched, two conditions must apply. First, on second line, the "local URI" must start with web/ (which corresponds with absolute web URI /foo/web/). Second, on first line, the real request URI must start with /foo/web/ too. Together this means, that the rule only matches when the file inside the web/ subfolder is requested directly from the browser, in which case we want to do a hard redirect.

RewriteCond $1 !^web/
RewriteCond $1 ^(.+\.(html|css|js|ico|png|bmp|jpg|gif))?$
RewriteRule ^(.*)$ web/$1 [L,NC]

我们要重定向到允许的,只有当我们还没有做它已经,所以第一个条件的内容。第二个条件指定掩码允许的内容。任何匹配这个面具将被轻轻地重定向,可能返回404错误,如果内容不存在了。

We want to redirect to allowed content only if we haven't done it already, hence the first condition. Second condition specifies mask for allowed content. Anything matching this mask will be softly redirected, possibly returning 404 error if the content doesn't exist.

RewriteRule !^web/ /foo/ [L,R]

这会是一个很难重定向到应用程序根目录对所有的URI不是开头网​​络/ (记住,可与开始只要求网站/ 在这一点上是内部重定向为允许使用的内容。

This will do a hard redirect to application root for all URIs not beginning with web/ (and remember, only requests that can begin with web/ at this point are internal redirects for allowed content.


我的code:

# disallow directory indexing
Options -Indexes

# turn mod_rewrite on
Options +FollowSymlinks
RewriteEngine on

# allow access to robots file
RewriteRule ^robots.txt$ - [NC,L]

# mangle core request handler address
# disallow direct access to core request handler
RewriteCond %{THE_REQUEST} !^(GET|POST)\ /asm/core/handleCoreRequest.php
RewriteRule ^core/handleCoreRequest.php$ - [L]
# allow access to request handler under alias
RewriteRule ^core/$ core/handleCoreRequest.php [NC,QSA,L]

# mangle GUI files adressing (move to application root folder)
# disallow direct access to GUI subfolder
RewriteCond %{THE_REQUEST} ^GET\ /foo/web/
RewriteRule ^web/(.*) /foo/$1 [L,R]
# allow access only to correct filetypes in appropriate locations
RewriteCond $1 ^$ [OR]
RewriteCond $1 ^(images/.+\.(ico|png|bmp|jpg|gif))$ [OR]
RewriteCond $1 ^(css/.+\.css)$ [OR]
RewriteCond $1 ^(js/.+\.js)$ [OR]
RewriteCond $1 ^(lib/js/.+\.js)$ [OR]
RewriteCond $1 ^(lib/css/.+\.css)$ [OR]
RewriteCond $1 ^(lib/(.+/)?images/.+\.(ico|png|bmp|jpg|gif))$
RewriteRule ^(.*)$ web/$1 [L,NC]

# hide all files not in GUI subfolder that are not whitelisted above
RewriteRule !^web/ /foo/ [L,R]


我不喜欢这种方式是什么,该应用程序的根文件夹必须是硬codeD的.htaccess文件(据我所知),因此该文件必须在应用程序生成的安装,而不是简单地复制


What I don't like about this approach is that the application root folder must be hardcoded in .htaccess file (as far as I know), so the file must be generated on application install, not simply copied.

这篇关于我如何获得的[L]标志的重写规则(的.htaccess)真正的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆