RewriteCond检查文件是否存在于子目录中

时间:2010-07-15 10:58:09

标签: iis mod-rewrite rewrite iirf

我正在使用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,但我无法确定如何检查webroot下是否存在REQUEST_FILENAME

我查看了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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我最终不得不换用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]