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.


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.

  • Thanks Jenny. I agree, it looks like mod_mime_magic should help, but it seems to be active already. I tried adding in a few more conditions to its conf/magic file with similar lack of progress. I may end up writing a small cgi layer that evaluates the content type, assigns mime type, and streams the file. – mmond Feb 16 '13 at 0:32

Two ways to get regular expression support -- You can use ForceType in LocationMatch, or you can use the T= flag in mod_rewrite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.