Mod_Rewrite 意外行为 L 标志 [英] Mod_Rewrite unexpected behavior L flag
问题描述
我的网络应用程序结构是:
/var/www/myapp/- www/- 索引.php- css.php- .htaccess
虚拟主机配置为:
服务器名称 www.example.orgDocumentRoot/var/www/myapp/wwwDirectoryIndex index.php index.html<目录/var/www/myapp/www>选项索引 FollowSymLinks MultiViews允许覆盖所有命令允许,拒绝允许所有人</目录></虚拟主机>
在/var/www/myapp/www/.htaccess 中有:
重写引擎开启重写基数/重写规则 css css.php [L,NC]重写规则 .* index.php</IfModule>
现在,如果我调用 www.example.org,我会正确地重定向到 index.php,但是如果我调用 www.example.org/css,我仍然总是被重定向到 index.php.
如果我删除RewriteRule .* index.php"这一行,然后调用 www.example.org/css,我将正确重定向到 css.php.
怎么了?非常感谢大家
======== 编辑 ==========
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/] 添加路径信息后缀:/var/www/sviluppo/mattia_dev/example/www/DEV_2 ->/var/www/sviluppo/mattia_dev/example/www/DEV_2/css/example1/test.css192.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/] 去掉每个目录的前缀:/var/www/sviluppo/mattia_dev/example/www/DEV_2/css/example1/test.css ->DEV_2/css/example1/test.css192.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/] 将模式 '^(DEV|TEST|PROD)\_[0-9]+\/(css|js|image|static)\/(.+)$' 应用到 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/] 重写'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/] 添加每个目录前缀:css/example1/test.css ->/var/www/sviluppo/mattia_dev/example/www/css/example1/test.css192.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/] 试图用/替换前缀/var/www/sviluppo/mattia_dev/example/www/192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (5) 去除匹配前缀:/var/www/sviluppo/mattia_dev/example/www/css/example1/test.css ->css/example1/test.css192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b7080058/initial] (4) 添加 subst 前缀:css/example1/test.css ->/css/example1/test.css192.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/] 内部重定向与/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/] 去掉每个目录的前缀:/var/www/sviluppo/mattia_dev/example/www/css/example1/test.css ->css/example1/test.css192.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/] 应用模式 '^(DEV|TEST|PROD)\_[0-9]+\/(css|js|image|static)\/(.+)$'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/] 去掉每个目录的前缀:/var/www/sviluppo/mattia_dev/example/www/css/example1/test.css ->css/example1/test.css192.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/] 将模式 '.*' 应用到 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/] 重写 '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/] 添加每个目录前缀:index.php ->/var/www/sviluppo/mattia_dev/example/www/index.php192.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/] 试图用/替换前缀/var/www/sviluppo/mattia_dev/example/www/192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (5) 去除匹配前缀:/var/www/sviluppo/mattia_dev/example/www/index.php ->索引.php192.168.1.8 - - [14/Jul/2012:19:07:21 +0200] [www.example.org/sid#b747b6c0][rid#b63188e8/initial/redir#1] (4) 添加 subst 前缀:索引.php ->/index.php192.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/] 内部重定向与/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/] 去掉每个目录的前缀:/var/www/sviluppo/mattia_dev/example/www/index.php ->索引.php192.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/] 应用模式 '^(DEV|TEST|PROD)\_[0-9]+\/(css|js|image|static)\/(.+)$'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/] 去掉每个目录的前缀:/var/www/sviluppo/mattia_dev/example/www/index.php ->索引.php192.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/] 将模式 '.*' 应用到 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/] 重写'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/] 添加每个目录前缀:index.php ->/var/www/sviluppo/mattia_dev/example/www/index.php192.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/] 初始 URL 等于重写 URL:/var/www/sviluppo/mattia_dev/example/www/index.php [忽略重写]
似乎发生了第一次重定向,但 mod_rewrite 并没有停止导致内部重定向.然后,更改后的 url 再次传递给 mod_rewrite,第二次重定向发生但现在匹配第二条规则.
我无法理解,因为我放置了 [L] 标志只是为了确保 mod_rewrite 停止.
下次谢谢
您遗漏了一个关于 ,是或多或少不是解决方案的一部分,只是注意到因为我引用了这个例子)
你真正想要使用的是 END
标志:
RewriteRule css css.php [END,NC]
但是,如果您有所需的 apache 版本(在 2.3.9 及更高版本中可用),请联系您的系统管理员.如果没有,则需要使用RewriteCond
.
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>
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>
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.
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]
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.
I can't understand it because I've placed the [L] flag just to ensure that the mod_rewrite stops.
Thanks another time
You are missing an important fact about the L
flag:
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
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:
I can't understand it because I've placed the [L] flag just to ensure that the mod_rewrite stops.
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).
If the URI changed L
will re-inject into the the next round (the outer loop) as the following technical details flowchart shows:
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:
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
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)
What you actually want to use is the END
flag:
RewriteRule css css.php [END,NC]
However contact your system administrator if you have the needed apache version for it (Available in 2.3.9 and later). If not, you need to operate with RewriteCond
.
这篇关于Mod_Rewrite 意外行为 L 标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!