Apache 递归地在子目录中查找匹配的文件名。
如果找不到文件,我希望递归地签入子目录,直到找到具有请求名称的文件并交付该文件 例如: 请求是:Apache 递归地在子目录中查找匹配的文件名。,apache,.htaccess,http,mod-rewrite,Apache,.htaccess,Http,Mod Rewrite,如果找不到文件,我希望递归地签入子目录,直到找到具有请求名称的文件并交付该文件 例如: 请求是: http://domain.com/file.txt 在子目录中递归查找,直到找到file.txt,然后交付: http://domain.com/foo/bar/file.txt 到目前为止,我唯一管理的是在找不到文件时触发: RewriteCond %{REQUEST_FILENAME} !-f 将其放入加载程序脚本中。首先,在应用程序vhost配置或.htaccess文件中进行重写 Re
http://domain.com/file.txt
在子目录中递归查找,直到找到file.txt,然后交付:
http://domain.com/foo/bar/file.txt
到目前为止,我唯一管理的是在找不到文件时触发:
RewriteCond %{REQUEST_FILENAME} !-f
将其放入加载程序脚本中。首先,在应用程序vhost配置或.htaccess文件中进行重写
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /loader.php?q=$1 [L,QSA]
然后在网站的根目录中有一个loader.php脚本,它将递归搜索一个文件,或者重定向到正确的URL,或者加载该文件,获取mime提示,设置正确的标题,并用该文件的内容进行响应。大概是这样的:
<?php
// Do some processing here to extract the proper file name
// from $_REQUEST['q'] into a variable called $filename
// Assume we have the $filename
$it = new RecursiveDirectoryIterator(__DIR__); // Start search where loader.php is located
foreach (new RecursiveIteratorIterator($it) as $file) {
if (pathinfo($file, PATHINFO_BASENAME) == $filename) {
$returnFile = $file;
break;
}
}
// Check if the file was found
// Do a proper redirect to the 404 page here if you need more
if (empty($returnFile)) {
header ("HTTP/1.0 404 Not Found");
die();
}
// You need to do some fancy magic here to set the proper content type
// We will return plain text for now
header("Content-Type:text/plain");
// Handle large files
set_time_limit(0);
$file = @fopen($file_path,"rb");
while(!feof($file))
{
print(@fread($file, 1024*8));
ob_flush();
flush();
}
/* EndOfScript */
就这样,我想。。。我根本没有测试过这个,所以它可能有很多错误!然后再次,你可以得到如何处理这个问题的主要图片 规则可以签入已知的子目录,但不能从每个路径生成目录列表。@anubhava:谢谢您提供的信息。不幸的是,子目录列表是未知的,并且可以动态更改。那么重写规则将没有任何帮助。最好在服务器端完成。