DefaultType directive in apache 2.4

We have just migrated from Apache 2.0.53 to Apache 2.4 Everything works perfectly for html/js/css files etc.

But we have a number of endpoints on our servers which return plain text. For example - an endpoint would return -

RIL~Reliance Industries~1234~abcd

By default the response for these do not contain any "content-type" but I am expecting "text/plain"

In Apache 2.0.53 we were using

DefaultType text/plain

But in Apache 2.4 , this directive is disabled. (Ref:http://httpd.apache.org/docs/2.4/mod/core.html#defaulttype)

I have a large number of requests and want a generic solution that I can apply to my apache so that if there is no content-type provided, then it should send "text/plain"

Any suggestions please.


Solution 1:

One solution is to use ForceType text/plain to set the Content-Type header to text/plain. This will make all responses return text/plain, however.

A very specific problem in using ForceType is that it breaks mod_mime's file extension detection. For example, files requested with .css or .js extensions won't have their Content-Type set to text/css or application/javascript and will instead have it set to text/plain: probably not what you want.

As noted in a chain on the apache mailing list, the reason why DefaultType was removed is because they are now recommending that the browser do the sniffing to figure out the content-type. If you're upgrading from apache 2.2 to 2.4, then you should try to just use it without adding it back in.

Solution 2:

Found the solution.

ForceType text/plain

This worked for me....

Solution 3:

Using ForceType at global level (meaning on all requests) will probably break other things like css, js...

This is common problem when serving path with html content having-no-extension (.html)

Simplified as not having a dot in the name:

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

Could be smarter to only check on last 5 characters:

<FilesMatch "^[^.]{0,5}$">
    ForceType text/html
</FilesMatch>