Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache 2.4 set mime type of file without extension

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 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.

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.

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.

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.

like image 824
Jonathan Katon Avatar asked Apr 01 '15 18:04

Jonathan Katon


People also ask

How do you specify the MIME type of a file?

3.2. Another way to get the MIME type of a file is by reading its content. We can determine the MIME type according to specific characteristics of the file content. For example, a JPG starts with the hex signature FF D8 and ends with FF D9. This is slower than the file extension approach due to the extra I/O efforts.

Is MIME type same as extension?

Slightly longer answer: Mime types and file extensions provide hints to how to deal with a file. Whereas file extensions are commonly used for your OS to decide what program to open a file with, Mime types are used by your browser to decide how to present some data (or the server on how to interpret received data).

Do all files have a MIME type?

There are too many issues with MIME types on different operating systems, different applications saving files differently, some files not having a MIME at all, and lastly, the fact that the extension and MIME could be altered by a malicious user or program.


1 Answers

Extensionless files only

This solution affects only extensionless, statically served files: (credit Eugene Kerner)

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

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}"

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”.

like image 193
Michael Allan Avatar answered Sep 23 '22 00:09

Michael Allan