为什么Apache mod_rewrite重写了两次我的URL?

时间:2013-06-13 20:03:48

标签: apache mod-rewrite url-rewriting

我需要将/admin/page/create网址重写为/backend/www/index.php/page/create,因此我使用下一个Apache重写规则和条件:

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !backend\/www

RewriteRule admin\/(.*)$ backend/www/index.php/$1 [NS,L]

但是,检查重写日志,我看到Apache正确地重写了URL,但最后它会考虑/page/create URL执行额外的重写,这与任何条件都不匹配,因此它失败并且我收到了“未指定输入文件。”错误。

如何阻止Apache执行额外的重写?我使用NS标志和RewriteCond %{IS_SUBREQ} f条件,它也执行子请求。

谢谢!

80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (2) init rewrite engine with requested uri /admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (1) pass through /admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] add path info postfix: /home/daniloaz/dev.daniloaz.com/httpdocs/admin -> /home/daniloaz/dev.daniloaz.com/httpdocs/admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] strip per-dir prefix: /home/daniloaz/dev.daniloaz.com/httpdocs/admin/page/create -> admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] applying pattern 'admin\/(.*)$' to uri 'admin/page/create'
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='!-f' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='!-d' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='!backend\/www' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='((admin\/(.*)$)|(admin$))' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='false' pattern='f' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (2) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] rewrite 'admin/page/create' -> 'backend/www/index.php/page/create'
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] add per-dir prefix: backend/www/index.php/page/create -> /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (2) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] strip document_root prefix: /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create -> /backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (1) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] internal redirect with /backend/www/index.php/page/create [INTERNAL REDIRECT]
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (2) init rewrite engine with requested uri /backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (1) pass through /backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] add path info postfix: /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php -> /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] strip per-dir prefix: /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create -> backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] applying pattern 'admin\/(.*)$' to uri 'backend/www/index.php/page/create'
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (1) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] pass through /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a4ce60/subreq] (2) init rewrite engine with requested uri /page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a4ce60/subreq] (1) pass through /page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a4ce60/subreq] (1) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] pass through /home/daniloaz/dev.daniloaz.com/httpdocs/page

2 个答案:

答案 0 :(得分:11)

好吧,我终于使用解决方法解决了这个问题。首先我尝试了these solutions,但是一次又一次地执行了子请求,并且重写失败了。

使用mod_rewrite,如果将规则放在.htaccess或全局配置(httpd.conf,Virtualhost配置等)中,则会有很小的差异。子请求是其中一个差异。只需将上面提到的相同规则放在Virtualhost配置文件中,而不是.htaccess,子查询停止,一切都像魅力一样开始工作。

我建议这篇有用的帖子在尝试解决任何与之相关的问题之前完全理解mod_rewrite的内部:http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

答案 1 :(得分:0)

我只是想放弃我的经验,以防有人达到我的目的:

我遇到了同样的问题。我尝试了每一个答案。每一个解决方案。达到我可以说“我真的完全理解重写引擎”的水平。但没有任何作用。我的网址仍在更改并显示查询字符串 在某个时刻,我说:“这可能是不可能的。必须是别的东西”。然后我尝试使用WGET而不是浏览器(firefox)获取URL。 我做了:

wget -qSO http://MY-THING

然后我意识到服务器中的NOTHING表示我在浏览器的地址栏中看到的URL。甚至更好,在日志文件中没有!

那么......它是如何以及为什么会改变的?显而易见的答案是“浏览器” 我尝试了另一个浏览器(chrome)并且正常工作! 所以我所做的就是用"forget this site"选项清理历史,每个人都很开心。

现在我不知道我什么时候修好它。可能是-Multiviews事物,或规则的标志,或INTERNAL REDIRECT部分​​......我真的不知道。但可能解决方案不存在。