使用htaccess检查另一个目录中的文件
好吧,我开始怀疑这是否可能。我一直在研究 htaccess 教程和示例,但我似乎无法让它正常工作。
我有一个脚本,可以创建在朋友网站上的目录中运行的动态图像。我可以访问 / 但我使用的实际目录位于 /mydir/ 中。为了显示图像,它们通过 PHP 输出为 png。我有以下 RewriteRule 设置来获取请求的文件名并运行 PHP 脚本。每个文件名代表一个单独的文件,其中包含在display.php脚本中使用的序列化对象
RewriteRule ^(.*)\.jpg$ display.php?file=$1
该部分工作正常,但是如果有人请求一个不存在的文件,它只会抛出PHP错误,因为脚本被调用但没有对象来使用它。
我现在想做的,完全失败的是让它检查文件是否存在于 ./Cache/ 目录中,然后像往常一样运行脚本,如果不存在,那么服务器只会抛出标准的 404。
我已经尝试过诸如
RewriteCond %{REQUEST_URI} ^(.*)\.jpg$
RewriteCond %{DOCUMENT ROOT}/img/Cache/%1.jpg -f
REQUEST_URI、REQUEST_URL、SCRIPT_FILENAME、SCRIPT_NAME 等的其他组合。我似乎无法想出正确的变量和正则表达式来处理这个问题,所以我向你们求助 帮助。最大的问题是不知道每个变量实际上是什么,所以我可以看到哪里出了问题。我会为了某种 var_dump() 而杀人,
Ok, I am starting to wonder if this is even possible. I have been pouring over htaccess tutorials and examples and I just can't seem to get it working right.
I have a script that creates dynamic images running in a directory on a friends website. I have access to / but the actual directory I am using is in /mydir/. In order to display the images they are output to png via PHP. I have the following RewriteRule setup to take the filename requested and run a PHP script. Each filename represents a separate file with a serialized object used in the display.php script
RewriteRule ^(.*)\.jpg$ display.php?file=$1
That part is working fine, however if someone requests a file that doesn't exist it just throws PHP errors because the script gets called but there is no object for it to use.
What I am trying to do now, and utterly failing at, is to have it check if the file exists in ./Cache/ directory then run the script as usual and if not then the server can just throw a standard 404.
I have tried things like
RewriteCond %{REQUEST_URI} ^(.*)\.jpg$
RewriteCond %{DOCUMENT ROOT}/img/Cache/%1.jpg -f
and other combinations of REQUEST_URI, REQUEST_URL, SCRIPT_FILENAME, SCRIPT_NAME, etc. I can't seem to come up with the proper variable and regex expression to handle this so I am turning to you guys for help. The biggest problem is not knowing what is actually in each of those variables so I can see where I am going wrong. I would kill for some kind of var_dump(),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此规则:
这需要放入
/mydir/img
文件夹中的 .htaccess 文件如果您将此文件夹移动到其他位置或重命名父文件夹,您还需要在
RewriteCond
行中更新它。如果您愿意,可以尝试这种方法,这种方法更灵活(无需指定父文件夹名称),但我不能保证这适用于您的设置(但它确实适用于我的电脑):
此处确定了当前的物理文件夹自动从请求的 URL 获取。但如果您有符号链接或其他一些设置,则可能会产生不正确的结果。另外..它还有1个条件..这使得它有点慢(1个额外的正则表达式来检查)。
Use this rule:
This needs to be placed into .htaccess file in
/mydir/img
folderIf you move this folder somewhere else or rename parent folder, you will need update it as well in
RewriteCond
line.If you wish you can try this approach, which is more flexible (no need to specify parent folder names) but I cannot guarantee that this will work on your setup (but it DOES work on my PC):
Here the current physical folder is determined automatically from requested URL. But if you have symbolic links or some other settings, it may produce incorrect results. Plus .. it has 1 more Condition .. which makes it a bit slower (1 extra regex to check).
我将使用 PHP 文件来检查它:
在 PHP 中使用
file_exists
,如果它不存在,请使用header("HTTP/1.0 404 Not Found");
或header('Location
到 404 图像I would use the PHP file to check it:
use
file_exists
in your PHP and if it does not exist, useheader("HTTP/1.0 404 Not Found");
orheader('Location
to a 404 image