mod_rewrite的意外行为了L位 [英] Mod_Rewrite unexpected behavior L flag

查看:145
本文介绍了mod_rewrite的意外行为了L位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序的结构是:

my web application structure is:

/var/www/myapp/
    - www/
        - index.php
        - css.php
        - .htaccess

虚拟主机被配置为:

the virtual host is configured as:

<VirtualHost *:80>
        ServerName www.example.org
        DocumentRoot /var/www/myapp/www
        DirectoryIndex index.php index.html
        <Directory /var/www/myapp/www>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

在/var/www/myapp/www/.htaccess有:

In /var/www/myapp/www/.htaccess there is:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule css css.php [L,NC]
    RewriteRule .* index.php
</IfModule>

现在,如果我叫www.example.org我正确地重定向到的index.php,但如果我叫www.example.org/css我还是始终重定向到的index.php。

Now, if I call www.example.org I'm correctly redirected to index.php, but if I call www.example.org/css I'm still redirected always to index.php.

如果我放弃行重写规则。*的index.php,然后调用www.example.org/css,我正确地重定向到css.php。

If I drop the line "RewriteRule .* index.php" and then call www.example.org/css, I'm correctly redirected to css.php.

有什么不对?
感谢所有非常

What's wrong? Thanks all very much

========编辑=========

======== EDIT =========

192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] add path info postfix: /var/www/sviluppo/mattia_dev/example/www/DEV_2 -> /var/www/sviluppo/mattia_dev/example/www/DEV_2/css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] strip per-dir prefix: /var/www/sviluppo/mattia_dev/example/www/DEV_2/css/example1/test.css -> DEV_2/css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] applying pattern '^(DEV|TEST|PROD)\_[0-9]+\/(css|js|image|static)\/(.+)$' to uri 'DEV_2/css/example1/test.css'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (2) [perdir /var/www/sviluppo/mattia_dev/example/www/] rewrite 'DEV_2/css/example1/test.css' -> 'css/example1/test.css'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] add per-dir prefix: css/example1/test.css -> /var/www/sviluppo/mattia_dev/example/www/css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (2) [perdir /var/www/sviluppo/mattia_dev/example/www/] trying to replace prefix /var/www/sviluppo/mattia_dev/example/www/ with /
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (5) strip matching prefix: /var/www/sviluppo/mattia_dev/example/www/css/example1/test.css -> css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (4) add subst prefix: css/example1/test.css -> /css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (1) [perdir /var/www/sviluppo/mattia_dev/example/www/] internal redirect with /css/example1/test.css [INTERNAL REDIRECT]
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] strip per-dir prefix: /var/www/sviluppo/mattia_dev/example/www/css/example1/test.css -> css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] applying pattern '^(DEV|TEST|PROD)\_[0-9]+\/(css|js|image|static)\/(.+)$' to uri 'css/example1/test.css'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] strip per-dir prefix: /var/www/sviluppo/mattia_dev/example/www/css/example1/test.css -> css/example1/test.css
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] applying pattern '.*' to uri 'css/example1/test.css'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (2) [perdir /var/www/sviluppo/mattia_dev/example/www/] rewrite 'css/example1/test.css' -> 'index.php'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] add per-dir prefix: index.php -> /var/www/sviluppo/mattia_dev/example/www/index.php
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (2) [perdir /var/www/sviluppo/mattia_dev/example/www/] trying to replace prefix /var/www/sviluppo/mattia_dev/example/www/ with /
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (5) strip matching prefix: /var/www/sviluppo/mattia_dev/example/www/index.php -> index.php
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (4) add subst prefix: index.php -> /index.php
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (1) [perdir /var/www/sviluppo/mattia_dev/example/www/] internal redirect with /index.php [INTERNAL REDIRECT]
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] strip per-dir prefix: /var/www/sviluppo/mattia_dev/example/www/index.php -> index.php
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] applying pattern '^(DEV|TEST|PROD)\_[0-9]+\/(css|js|image|static)\/(.+)$' to uri 'index.php'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] strip per-dir prefix: /var/www/sviluppo/mattia_dev/example/www/index.php -> index.php
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] applying pattern '.*' to uri 'index.php'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (2) [perdir /var/www/sviluppo/mattia_dev/example/www/] rewrite 'index.php' -> 'index.php'
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (3) [perdir /var/www/sviluppo/mattia_dev/example/www/] add per-dir prefix: index.php -> /var/www/sviluppo/mattia_dev/example/www/index.php
192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b6310db8/initial/redir#2] (1) [perdir /var/www/sviluppo/mattia_dev/example/www/] initial URL equal rewritten URL: /var/www/sviluppo/mattia_dev/example/www/index.php [IGNORING REWRITE]

似乎发生了第一次重定向,但mod_rewrite的不会停止引起内部重定向。然后,更改URL,传递到mod_rewrite的另一个时间和发生第二重定向,但现在符合第二条规则。

Seems that a first redirect occurs but the mod_rewrite doesn't stop causing an internal redirect. Then, the changed url, is passed to mod_rewrite another time and a second redirect occured but now matching the second rule.

我不明白,因为我已经放在了[L]标志只是为了确保mod_rewrite的停止。

I can't understand it because I've placed the [L] flag just to ensure that the mod_rewrite stops.

由于其他时间

推荐答案

您缺少一个重要的事实有关的 标志

You are missing an important fact about the L flag:

这是非常重要的,如果你使用<一个href=\"http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule\"><$c$c>RewriteRule在其中的一个指令[的.htaccess &LT;目录&GT; ]背景,即您采取明确的步骤避免循环的规则,而不是在[L]标志仅数终止了一系列执行规则

It is therefore important, if you are using RewriteRule directives in one of these [.htaccess, <Directory>] contexts, that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of rules

来源:大号|最后(标志);由我粗体

From: L|last (Flag); bold by me

这意味着,只有通过使用不必prevent的INTERAL重定向你想要的效果。在内部重定向在这里是因为它一定会发生的,你们用自己的的.htaccess 配置中指定它。在标志的的右旗至prevent的内部重定向

That means, only by using the L does not have your desired effect to prevent the interal redirect. The INTERNAL REDIRECT happens here because it must happen, you have specified it with your .htaccess configuration. The L flag is not the right flag to prevent the INTERNAL REDIRECT.

让我们看看你的问题密切,实际上发生了什么:

Let's look closer at your question and what actually happens:

我不明白,因为我已经放在了[L]标志只是为了确保mod_rewrite的停止。

I can't understand it because I've placed the [L] flag just to ensure that the mod_rewrite stops.

只是,你有标记的错误undestanding。它只会停止当前重写,这意味着,它下面的重写规则指令不会在本轮(内环)进行处理。

Is just that you have the wrong undestanding of the L flag. It will only stop for the current rewriting, meaning, the RewriteRule directives beneath it are not going to be processed in the current round (the inner loop).

如果URI改变将重新注入到下一轮(外循环),如下面的技术细节流程图显示:

If the URI changed L will re-inject into the the next round (the outer loop) as the following technical details flowchart shows:

要突出显示在标志踢和,其中内部重定向情况发生,这是相同的图形与一些注释为您specfic(第一个)URI改写:

To highlight where the L flag kicks in and where the INTERNAL REDIRECT happens, this is the same graphic with some annotations for your specfic (first) URI rewrite:

这表明,标记只退出内部循环,但如果URI已被重写(改变) - 在你的情况 - 外环负责的改URI会通过的再次的你的所有重写规则。

It shows that the L flag only exits the inner loop but if the URI has been rewritten (changed) - as in your case - the outer loop takes care that the changed URI will be passed again to all your rewrite rules.

相反,你可能要制定一个条件从手动显示的那部分下面的例子:

Instead you might want to formulate a condition as the following example from that part of the manual shows:

RewriteBase /
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php?req=$1 [L,PT]

PT 都有自己的手册进入,是或多或少不解决方案的一部分,只是指出,因为我引用这个例子原样)

(PT has it's own manual entry, is is more or less not part of the solution, just noting because I quoted the example as-is)

您真的想使用什么是 END 标记:

What you actually want to use is the END flag:

RewriteRule css css.php [END,NC]

不过与系统管理员联系,如果你有这方面需要的Apache的版本(可在2.3.9或更高版本)。如果没有,你需要运行<一个href=\"http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond\"><$c$c>RewriteCond.

这篇关于mod_rewrite的意外行为了L位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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