如何正确拦截PHP中的404错误?

时间:2022-11-29 13:25:37

Essentially, what I am doing is intercepting a 404 using ErrorDocument in .htaccess, and pointing it at getFile.php. getFile then grabs the file name out of the url and searches for it in the mysql database, getting the data and applying appropriate headers.

基本上,我正在做的是使用.htaccess中的ErrorDocument拦截404,并将其指向getFile.php。然后,getFile从url中获取文件名,并在mysql数据库中搜索它,获取数据并应用适当的头文件。

In my dev environment (up-to-date LAMP server running under ubuntu) everything seems to work fine, but the site is being hosted on GoDaddy, and there seems to be some issue with the headers returned. I think GoDaddy returns a 404, then sends the data anyway.

在我的开发环境(在ubuntu下运行的最新LAMP服务器)一切似乎工作正常,但该网站正在GoDaddy上托管,并且返回的标头似乎存在一些问题。我认为GoDaddy会返回404,然后无论如何都要发送数据。

The result is when I right click on a link that is to be handled in this manner, I can 'Save As', and everything works fine. If the file is an image, it opens no problem. But if I just click the link and it is a file that is to be downloaded rather than viewed in a browser, I get 'File Not Found' errors, across all browsers. Firebug Console claims it is a 404 error, regardless of whether the file successfully transfers. My code is the following:

结果是当我右键单击要以这种方式处理的链接时,我可以“另存为”,一切正常。如果文件是图像,则打开没有问题。但是,如果我只是点击链接,它是一个要下载而不是在浏览器中查看的文件,我会在所有浏览器中收到“找不到文件”错误。 Firebug控制台声称它是404错误,无论文件是否成功传输。我的代码如下:

$folder = explode( "/" , $_SERVER["REQUEST_URI"] );
$pageName = $folder[sizeof($folder)-1];

$sql = "SELECT data, mimeType, OCTET_LENGTH(data) AS size FROM tblFiles WHERE fileName = '".$pageName."' AND active=1;";

$result = mysql_query("$sql") or die();

$data = mysql_fetch_array($result);

header("HTTP/1.0 200 OK");
header("Content-Type: ".$data['mimeType']);
header("Content-Length: ".$data['size']);

echo $data['data'];

And the .htaccess file:

和.htaccess文件:

ErrorDocument 404 /FILES/dbFiles/getFile.php

Now this is the full .htaccess file present at /FILES/dbFiles. I don't know if GoDaddy is expecting more stuff in there, but it seems to work just as written on my own server.

现在这是/ FILES / dbFiles中存在的完整.htaccess文件。我不知道GoDaddy是否期待更多的东西,但它似乎与我自己的服务器上写的一样。

I've tried numerous combinations of header info with no positive effect.

我已尝试过多种标题信息组合,没有任何正面效果。

My code apparently works, but I can't seem to prevent that 404 from happening.

我的代码显然有效,但我似乎无法防止404发生。

Thanks! -Jer

2 个解决方案

#1


4  

Try this in your /FILES/dbFiles/.htaccess file (without the ErrorDocument part):

在/FILES/dbFiles/.htaccess文件中尝试这个(没有ErrorDocument部分):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ getFile.php?file=$0 [QSA,L]

This will direct all non-existing URLs to your getFile.php script without generating a 404. Make sure you do output a 404 if the script does not find the content in your database.

这会将所有不存在的URL定向到getFile.php脚本而不生成404.如果脚本未在数据库中找到内容,请确保输出404。

Inside your PHP file, use:

在PHP文件中,使用:

if(@$_GET['file']) {

    //Your code here.  Use $_GET['file'] instead of $_SERVER["REQUEST_URI"]

} else {
    //Weird.  This file was directly accessed.
    die();
}

#2


0  

Oh man, nice solution. My situation was this: I was streaming javascript content from another directory when a requested .js file didn't exist, and while the actual content of the javascript was returning just fine the status was 404.

哦,伙计,好的解决方案。我的情况是这样的:当一个请求的.js文件不存在时,我正在从另一个目录中传输javascript内容,而当javascript的实际内容返回时,状态为404。

For my purposes this is what helped, hopefully it helps others too:

为了我的目的,这是有帮助的,希望它也有助于其他人:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) route/index.php [L,QSA]

#1


4  

Try this in your /FILES/dbFiles/.htaccess file (without the ErrorDocument part):

在/FILES/dbFiles/.htaccess文件中尝试这个(没有ErrorDocument部分):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ getFile.php?file=$0 [QSA,L]

This will direct all non-existing URLs to your getFile.php script without generating a 404. Make sure you do output a 404 if the script does not find the content in your database.

这会将所有不存在的URL定向到getFile.php脚本而不生成404.如果脚本未在数据库中找到内容,请确保输出404。

Inside your PHP file, use:

在PHP文件中,使用:

if(@$_GET['file']) {

    //Your code here.  Use $_GET['file'] instead of $_SERVER["REQUEST_URI"]

} else {
    //Weird.  This file was directly accessed.
    die();
}

#2


0  

Oh man, nice solution. My situation was this: I was streaming javascript content from another directory when a requested .js file didn't exist, and while the actual content of the javascript was returning just fine the status was 404.

哦,伙计,好的解决方案。我的情况是这样的:当一个请求的.js文件不存在时,我正在从另一个目录中传输javascript内容,而当javascript的实际内容返回时,状态为404。

For my purposes this is what helped, hopefully it helps others too:

为了我的目的,这是有帮助的,希望它也有助于其他人:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) route/index.php [L,QSA]