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

发布于 2024-09-10 04:35:01 字数 1879 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埖埖迣鎅 2024-09-17 04:35:01

嗯...我以前没听说过 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

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 be APPL_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 how mod_rewrite does things), but maybe it'll work:

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
薄暮涼年 2024-09-17 04:35:01

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

I ended up having to swap to using the Helicon Tech ISAPI_Rewrite 3 filter.

The htaccess file I ended up using was:

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文