Use regex or wildcard to set Apache’s AddType Directive

apache-2.2httpd.confmime

I manage an internal Apache2 system enabling HTTP browsing and downloads of a connected file system. I do not have control of the files, file types, or file names of the content being uploaded by the team.

Anything that is possible to render in the browser, I do. Text, images, xml, etc all work fine because I assign an explicit MIME type in httpd.conf's AddType directive. But some of the content is programmatically generated and does not have an extension I can predict, like: file.name.text_1234. These files are always text/plain, but I am not able to assign that content type without adding 10k entries AddType .text_NNNN entries to http.conf or /etc/mime.types. And even if I did, I stil may later encounter extensions like .text_123456789 or .text_abc123def456 etc, etc, so explicit entries are not practical.

Is there any way to assign the AddType via wildcard or regex or even force anything not otherwise set to be content type text/plain?

I've tried using related directives like DefaultType, ForceType, FilesMatch, Files, and Location to either globally override or match some condition that will assign this type, but nothing has worked. As soon I add:

AddType text/plain .blabla_123456

..then some.file.name.blabla_123456 renders perfectly, after restarting Apache.

Best Answer

I think the module mod_mime_magic may be what you're looking for. It'll look at the contents of the file and guess what mime type it should be. It should be correct for pretty much all text files.