【问题标题】:Apache mod_rewrite RewriteCond to by-pass static resources not workingApache mod_rewrite RewriteCond 绕过静态资源不起作用
【发布时间】:2010-04-22 03:30:58
【问题描述】:

我一生都无法理解为什么这个 RewriteCond 会导致每个请求都发送到我的 FastCGI 应用程序,而实际上它应该让 Apache 提供静态资源。

我已将hello.txt 文件添加到我的 DocumentRoot 以进行演示。

文本文件:

ls /Sites/CioccolataTest.webapp/Contents/Resources/static  
hello.txt

VirtualHost 及其重写规则:

AppClass /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest -port 5065
FastCgiExternalServer /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest.fcgi -host 127.0.0.1:5065
<VirtualHost *:80>
    ServerName cioccolata-test.webdev
    DocumentRoot /Sites/CioccolataTest.webapp/Contents/Resources/static

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest.fcgi/$1 [QSA,L]
</VirtualHost>

即使使用 -f,Apache 也会引导对该文本文件的请求(即访问 http://cioccolata-test.webdev/hello.txt 返回我的应用程序,而不是文本文件)。

作为概念证明,我将 RewriteCond 更改为:

RewriteCond %{REQUEST_URI} !^/hello.txt

这使它正确地为文本文件提供服务,并允许所有其他请求访问 FastCGI 应用程序。

为什么我的原始版本不起作用?我需要告诉 apache 将 DocumentRoot 中的每个文件都作为静态资源提供服务,但如果该文件不存在,它应该重写对我的 FastCGI 应用程序的请求。

注意:正在运行的 FastCGI 应用程序位于 /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest(没有 .fcgi 前缀)... .fcgi 前缀仅用于告诉 fastcgi 模块将请求定向到应用程序。

【问题讨论】:

  • mod_rewrite 语法在我看来是正确的,因为它的价值。我在生产代码中有相同的构造。我的也有: RewriteCond %{REQUEST_FILENAME} !-d

标签: apache mod-rewrite fastcgi


【解决方案1】:

我找到了一种让它工作的方法,但在我 6 年的 Web 开发生涯中,我从来没有这样做过。在检查它是否是文件之前,我在 REQUEST_FILENAME 前面加上了 %{DOCUMENT_ROOT} 前缀。

FastCgiServer /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest -port 5065
<VirtualHost *:80>
    ServerName cioccolata-test.webdev
    DocumentRoot /Sites/CioccolataTest.webapp/Contents/Resources/static

    RewriteEngine On

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /Sites/CioccolataTest.webapp/Contents/MacOS/CioccolataTest [QSA,L]
</VirtualHost>

出于某种原因,它将所有路径都视为绝对文件系统路径。通常 apache 会处理相对于文档根目录的路径,不是吗?

我想知道这是否与我的 fastcgi 应用程序在绝对路径上被引用的事实有关(我认为它必须如此)。我是 fastcgi 世界的新手。

【讨论】:

  • 在您的文档中查找每个目录的上下文和 RewriteBase。
【解决方案2】:

%{REQUEST_FILENAME} 在 VHost 上下文中不可用,因为 mod_rewrite 在设置此变量的阶段之前挂钩到请求。预先添加 DOCUMENT_ROOT 是解决此问题的好方法。

【讨论】:

    【解决方案3】:

    我猜 REQUEST_FILENAME 在这种情况下可能不是您想要的。增加你的 RewriteLogLevel ,它会告诉你为什么它正在做它正在做的任何事情。

    【讨论】:

    • 啊,我想知道是否有某种方法可以调试这些变量是什么!我会试试这个并回帖,谢谢。
    • 日志级别设置为 3 和 4,我得到相同的输出,所以我认为它不会更高。因为我不相信我可以在 cmets 中粘贴代码块,所以这里有一个指向单个请求的日志输出的链接。 w3style.co.uk/~d11wtq/rewrite_log.txt 请注意,它会查看输入“/hello.txt”的 RewriteCond,并确定它与规则匹配,因此必须重写为 FastCGI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多