【问题标题】:RewriteCond Check if file exists in a subdirectoryRewriteCond 检查文件是否存在于子目录中
【发布时间】:2011-03-16 08:31:36
【问题描述】:

我正在使用Iirf v2.0

我的目录结构如下:

/
/library
/library/index.php
/webroot
/webroot/images
/Iirf.ini

我有一个包含我的应用程序的库文件夹、一个 webroot 文件夹(其中包含图像、样式表等)和一个 Iirf.ini 配置文件。

我想将所有请求重定向到 /library/index.php 如果该文件在 webroot 下不存在。

例如:

Request             Response
/images/blah.png -> /webroot/images/blah.png
/news            -> /library/index.php

我的 Iirf.ini 配置有:

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /library/index.php [L]

它将所有内容重定向到/library/index.php,但我无法确定如何检查REQUEST_FILENAME 是否存在于webroot 下。

我查看了this question,但我无权访问DOCUMENT_ROOT。它给了我以下信息(取自日志):

Thu Jul 15 11:46:21 -   760 - ReplaceServerVariables: VariableName='REQUEST_FILENAME' Value='C:\web\favicon.ico'
Thu Jul 15 11:46:21 -   760 - ReplaceServerVariables: in='%{DOCUMENT_ROOT}/webroot/%{REQUEST_FILENAME}' out='DOCUMENT_ROOT/webroot/C:\web\favicon.ico'

任何帮助将不胜感激。

--- 编辑 --

在阅读更多内容和 Tim 的建议后,我更新了我的配置:

RewriteCond $0 !^/webroot
RewriteRule ^.*$ /webroot$0 [I]

RewriteCond $0 !-f
RewriteRule ^/webroot/(.*)$ /library/index.php [I,L,QSA]

它正确地传递给/library/index.php,但它仍然不检查现有文件(即使它似乎说它确实如此)。

Thu Jul 15 14:47:30 -  3444 - EvalCondition: checking '/webroot/images/buttons/submit.gif' against pattern '!-f'
Thu Jul 15 14:47:30 -  3444 - EvalCondition: cond->SpecialConditionType= 'f'
Thu Jul 15 14:47:30 -  3444 - EvalCondition: Special: it is not a file

我想我得联系过滤器的作者了。

【问题讨论】:

  • 嗯,在第二种情况下RewriteCond %{APPL_PHYSICAL_PATH}$0 !-f 怎么样(相对 URL 不是文件路径,这在 mod_rewrite 中也不起作用)? (或者你确定%{APPL_PHYSICAL_PATH} 确实不是我所希望的?)
  • 使用%{APPL_PHYSICAL_PATH} 会产生类似C:\path\to\folder\webroot/images/buttons/submit.gif 的路径,您认为这是正确的,但它仍然失败。
  • 嗯,(查看源代码)它依赖于 Windows API 函数 CreateFile 的返回值来检查文件是否存在......这应该没有问题找到那个路径,所以我不明白,呸。
  • 是的,我也不明白。我已经在网站的留言板上发布了一条消息。希望作者能回复我。感谢您的帮助。

标签: iis mod-rewrite rewrite iirf


【解决方案1】:

嗯...我以前没有听说过 IIRF,很酷的东西。在浏览了文档以了解它与 mod_rewrite 之间的区别后,我有两件事您可以尝试。

首先是在您找到的答案中将%{DOCUMENT_ROOT} 换成%{APPL_PHYSICAL_PATH}DOCUMENT_ROOT 是一个 Apache 服务器变量,据我所知,相应的 IIS 变量应该是 APPL_PHYSICAL_PATH。根据 IIRF 文档,我知道这个变量是可用的,但不可否认,我不能 100% 确定它是否指向您的站点根目录。

另一个是执行以下操作,根据我是否正确理解文档、您的index.php 文件如何获取相关路径信息来处理请求以及许多其他事情,这可能会或可能不会起作用。诚然,我认为这是一个不太理想的解决方案(与我最初根据 mod_rewrite 的工作方式所考虑的方案相比),但也许它会起作用:

RewriteEngine ON

# This should rewrite to /webroot/whatever then restart the ruleset,
# apparently...On Apache in a per-dir context, this would alter the
# %{REQUEST_FILENAME} for the next run-through. I'm assume it does
# here too, but I might be wrong.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}      !^/webroot
RewriteRule ^.*$ /webroot/$0

# The file still doesn't exist, rewrite it back to its original form,
# but move on to the next rule instead of restarting processing. This
# may not even be necessary, but I was hoping this rewrite would have
# side-effects that would make it as if the above rewrite didn't happen.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(/webroot/)?(.*)$ $0 [NI]

# Now, if it still doesn't exist, we'll rewrite it to our
# /library/index.php file, but this may not work based on how you
# get the original request information. Adding the [U] flag will 
# create a new header that preserves the "original" URL (I'm not
# sure what it takes the value from if the URL has already been
# rewritten in a previous step), which might be useful.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /library/index.php

【讨论】:

    【解决方案2】:

    我最终不得不改用Helicon Tech ISAPI_Rewrite 3 过滤器。

    我最终使用的 htaccess 文件是:

    RewriteEngine On
    
    # Check whether the file exists and if not, check whether the request starts
    # with webroot. Prepend webroot if it doesn't.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI}      !^webroot
    RewriteRule ^.*$ webroot/$0 [NI]
    
    # Check whether the file exists, if not, send the request off to library/index.php
    RewriteCond %{DOCUMENT_ROOT}/$0 !-f
    RewriteRule ^(webroot/)?(.*)$ library/index.php [I,L,QSA]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-04
      • 2013-04-07
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2018-05-08
      相关资源
      最近更新 更多