【问题标题】:Header add Content-Disposition "attachment" causes Internal Server error标头添加内容处置“附件”导致内部服务器错误
【发布时间】:2012-04-08 23:03:43
【问题描述】:

由于我的电子书阅读器(Sony PRS-T1)的内置浏览器非常愚蠢,并且想将 .epub 文件作为文本文件打开而不是下载它们,我试图强制浏览器下载 .epub 文件这个 .htaccess 文件:

<FilesMatch "\.(?i:epub)$">
  ForceType application/octet-stream
  Header add Content-Disposition "attachment"
</FilesMatch>

但是,这会导致内部服务器错误:

内部服务器错误

服务器遇到内部错误或配置错误,并且 无法完成您的请求。

请联系服务器管理员 webmaster@localhost 和 通知他们错误发生的时间,以及您可能会做的任何事情 这样做可能会导致错误。

有关此错误的更多信息可能在服务器错误中可用 记录。

当我省略 Header add Content-Disposition "attachment" 时,没有错误 - 但是,浏览器不会下载文件:(

我做错了吗?内部服务器错误来自哪里?

[编辑 2013-04-11]

我刚刚为这个帖子赢得了“热门问题徽章”,这让我想起了添加一些信息。

我终于设法使用以下 php 函数在 Sony 的 PRS-T1 浏览器上强制下载

function startDownload($path, $mimeType) {
if(!file_exists($path)) {
// File doesn't exist, output error
exit('file not found');
} else {
$size = filesize($path);
$file = basename($path);

// Set headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
}

exit();
}

希望有一天能对某人有所帮助。

【问题讨论】:

标签: apache .htaccess download


【解决方案1】:

这可能是答案:

http://diogomelo.net/node/24

默认情况下,Apache 上未启用模块标头。所以,我们必须 手动启用它。

要启用此模块,请以 root 身份登录,然后从以下位置创建符号链接 mods-available/headers.load 到 mods-enabled。之后,重新加载apache 它完成了。为此,我使用了这个命令。

su - cd
/etc/apache2/mods-enabled/ ln -s ../mods-available/headers.load
headers.load sh /etc/init.d/apache2 force-reload

【讨论】:

  • 如何在 Windows 上的 Apache 上实现?
  • @Gregra 打开httpd.conf 并取消注释此行:LoadModule headers_module modules/mod_headers.so,方法是删除# 字符。然后重启你的 apache 服务器。
【解决方案2】:

在您的 htaccess 文件中使用它之前,您可能还需要确保已启用 headers 模块。如果未启用 headers 模块,以下行将生成错误:

Header set Content-Disposition "attachment"

这是一个示例,它仅在启用 headers 模块时才强制下载 mp3 文件:

<IfModule mod_headers.c>
    <FilesMatch "\.(mp3|MP3)$">
        ForceType audio/mpeg
        Header set Content-Disposition "attachment"
        Allow from all
    </FilesMatch>
</IfModule>

注意:它不会启用模块,如果模块未启用,它只会忽略 IfModule 标记内的任何内容。

要启用 apache 模块,您需要编辑 httpd.conf 文件或在 wamp 服务器中单击 wamp 托盘图标并选择“Apache -> Apache Modules -> headers_module”或确保已选中。

【讨论】:

    【解决方案3】:

    对于 ubuntu,有一个启用 apache2 headers 模块的快捷方式,使用:

    sudo a2enmod headers
    

    问题解决了^_^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-14
      • 2013-12-24
      • 1970-01-01
      • 2015-06-09
      • 2014-11-26
      • 1970-01-01
      • 2010-10-07
      • 2014-12-14
      相关资源
      最近更新 更多