Apache 2.4设置MIME类型的文件,不带扩展名 [英] Apache 2.4 set mime type of file without extension

查看:564
本文介绍了Apache 2.4设置MIME类型的文件,不带扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在RedHat 6.4服务器上从Apache 2.2升级到2.4,并遇到了mime类型的问题.

I have upgraded from Apache 2.2 to 2.4 on a RedHat 6.4 server and came across an issue with mime types.

Apache具有 DefaultType指令.在Apache 2.2中,我将其设置为"text/plain".我有一个网页,列出了给定目录中的所有文件,用户可以单击以查看文件.此目录包含所有类型的不同文件扩展名和一些不带扩展名的文件.单击文件后,它将在一个格式正确的新窗口中打开.没有任何代码执行此操作.严格来说,浏览器会打开文件并根据其内容类型决定要执行的操作.

Apache has a DefaultType Directive. In Apache 2.2 I set this to "text/plain". I have a webpage that lists all files in a given directory and the user can click to view the files. This directory contains all types of different file extensions and some files with no extensions. When a file was clicked, it would open up in a new window nicely formatted. There is not any code doing this. It is strictly the browser opening the file and deciding what to do based on its content type.

此指令已在Apache 2.4中禁用. Apache文档网站指示用户使用mime.types配置文件和 AddType指令以配置媒体类型.

This directive has been disabled in Apache 2.4. The Apache documentation website instructs the user to to use the mime.types configuration file and the AddType Directive to configure media types.

我的问题是如何为没有扩展名的文件分配文本/纯文本" MIME类型?在Apache 2.2中,默认情况下会通过DefaultType指令为这些文件提供文本/纯文本"内容类型.在Apache 2.4中,由于无法再使用此指令,因此我无法弄清楚该如何做.我不想使用 ForceType指令,因为它会覆盖其他已经定义的mime类型.

My question is how do I assign the "text/plain" mime type to files with no extension? In Apache 2.2 those files would be given the "text/plain" content type by default through the DefaultType Directive. In Apache 2.4 I cannot figure out how to do this since I can't use this directive anymore. I do not want to use the ForceType Directive because it would override other already defined mime types.

我可以创建一个php包装器来加载文件并分配内容类型,但是我希望将逻辑保持在所有其他mime类型定义所在的apache中.

I could create a php wrapper that loads the file and assign a content type but I'd prefer to keep the logic within apache where all other mime type definitions are located.

任何帮助将不胜感激.如果需要其他信息,请告诉我.

Any help would be appreciated. If additional information is needed please let me know.

推荐答案

仅无扩展名文件

此解决方案仅影响无扩展名的静态服务文件:(信贷 Eugene Kerner )

<FilesMatch "^[^.]+$">
    ForceType text/plain
    </FilesMatch>

任何未知内容

这会影响任何在没有Content-Type头的情况下将被发送的响应.换句话说,它模仿了旧的DefaultType指令的行为:

Any unknown content

This one affects any response that would otherwise be transmitted without a Content-Type header. In other words, it mimics the behaviour of the old DefaultType directive:

Header set Content-Type "text/plain" "expr=-z %{CONTENT_TYPE}"

这里应该可以使用setifempty代替-z表达式.但是它失败并覆盖每个响应中的标头(是否为空).我不知道为什么 Eric a>表示这是因为Content-Type标头没有添加到直到最后一秒".

It should be possible to use setifempty here instead of the -z expression. But it fails and overwrites the header in every response, empty or not. I don’t know why. Eric Covener says it’s because the Content-Type header isn’t added "until the very last second".

这篇关于Apache 2.4设置MIME类型的文件,不带扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆