RewriteCond 检查子目录中是否存在文件
我正在使用 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 下。
我看过这个问题但我不知道无法访问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'
任何帮助将不胜感激。
--- 编辑 --
经过更多阅读和蒂姆的建议后,我更新了我的配置:
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
我想我必须联系过滤器的作者。
I'm using Iirf v2.0.
I have the following directory structure:
/
/library
/library/index.php
/webroot
/webroot/images
/Iirf.ini
Where I have a library folder which contains my application, a webroot folder (which contains images, stylesheets etc) and an Iirf.ini config file.
I'm wanting to redirect all requests to /library/index.php if the file doesn't exist under webroot.
eg:
Request Response
/images/blah.png -> /webroot/images/blah.png
/news -> /library/index.php
My Iirf.ini config has:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /library/index.php [L]
Which redirects everything to /library/index.php
but I'm having trouble working out how to check if the REQUEST_FILENAME
exists under webroot.
I've looked at this question but I don't have access to DOCUMENT_ROOT
. It gives me the following (taken from the log):
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'
Any help would be greatly appreciated.
--- EDIT --
I've updated my config after more reading and the suggestions of Tim to be:
RewriteCond $0 !^/webroot
RewriteRule ^.*$ /webroot$0 [I]
RewriteCond $0 !-f
RewriteRule ^/webroot/(.*)$ /library/index.php [I,L,QSA]
And it passes to /library/index.php
correctly but it still doesn't check for an existing file (even though it seems to say that it does).
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
I think I'm going to have to contact the author of the Filter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯...我以前没听说过 IIRF,很酷的东西。浏览文档了解它与 mod_rewrite 之间的区别后,我有两件事您可以尝试。
第一种方法是在您找到的答案中将
%{DOCUMENT_ROOT}
替换为%{APPL_PHYSICAL_PATH}
。DOCUMENT_ROOT
是一个 Apache 服务器变量,据我所知,相应的 IIS 变量应该是APPL_PHYSICAL_PATH
。根据 IIRF 文档,我知道此变量可用,但不可否认,我不能 100% 确定它是否指向您的站点根目录。另一种方法是执行以下操作,这可能会或可能不会起作用,具体取决于我是否正确理解文档、您的
index.php
文件如何获取处理请求的相关路径信息以及主机其他的事情。诚然,我认为这是一个不太理想的解决方案(与我最初基于mod_rewrite
的工作方式所想的相比),但也许它会起作用:Hmm...I hadn't heard about IIRF before, cool stuff. After browsing through the documentation to see what the differences between it and
mod_rewrite
are, I have two things you could try.The first is to swap out
%{DOCUMENT_ROOT}
for%{APPL_PHYSICAL_PATH}
in the answer that you found.DOCUMENT_ROOT
is an Apache server variable, and from what I can tell the corresponding IIS variable should beAPPL_PHYSICAL_PATH
. I know based on the IIRF documentation that this variable is available, but admittedly I'm not 100% sure whether or not it points to your site root.The other is to do the following, which again may or may not work based upon whether I understood the documentation correctly, how your
index.php
file gets the relevant path information to process the request, and a host of other things. Admittedly I think this is a less than ideal solution (compared to what I had originally thought to do based on howmod_rewrite
does things), but maybe it'll work:我最终不得不改用 Helicon Tech ISAPI_Rewrite 3 过滤器。
我最终使用的 htaccess 文件是:
I ended up having to swap to using the Helicon Tech ISAPI_Rewrite 3 filter.
The htaccess file I ended up using was: