从 apache 设置来看,在这种情况下,您最好的选择是升级到 1.4.24+,否则这些重写规则将开始变得复杂,因为您需要指向存在的每个文件。例如这样的事情:
url.rewrite = (
"^/index.php$" => "/index.php",
"^/page1.php$" => "/page1.php",
"^/page2.php$" => "/page2.php",
.
.
.
"^/pageN.php$" => "/page.php",
.
.
.
"^/uploads/uploadedfile.pdf$" => "/uploads/uploadedfile.pdf",
.
.
.
"(.*)" => "/index.php?req=$1"
)
如果您的应用程序没有很多用户需要直接访问的文件,您可以如上所述解决此问题。但是,一旦您开始处理上传的内容或任何动态内容,您就会遇到问题。
就我个人而言,我过去总是通过添加任何我不想轻松重写的内容到子目录中来避免此类问题,然后您可以使用通配符(这在您的情况下可能有效也可能无效),如下所示:
url.rewrite = (
"^/content/(.*)$" => "/content/$1",
"(.*)" => "/index.php?req=$1"
)
如果以上都没有任何用处,您可以使用 php 脚本来提供所有动态内容,但这会带来一系列问题和不灵活。例如:
<?php
$fullPath = '/path/to/files/' . $_GET['filename'];
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
} else
exit('File Not Found');
?>
希望有帮助