Moving Beyond SSH: Changing Content-Types

Published on Dec 2, 2009   //  Development

Moving Beyond SSH

When a server transmits a file to the user, additional “header” data is sent along with it. This header data contains various information about the session. One crucial part is the content-type, which contains an internet media type (commonly: “MIME type”) specification. This media type tells the browser what kind of file is being retrieved, and provides a guideline for handling the file (eg. display the file, offer to download, etc).

By default, Apache associates file name extensions to the appropriate media type. You can view these associations in the file /usr/local/apache2/conf/mime.types. However, what if you need to change how a file should be handled, or specify how an unspecified extension should be handled? It is recommended that you do not edit the mime.types file, as the file is likely to be updated during Apache upgrades, which will undo all your changes. Instead, we’ll use an .htaccess file to make our changes.

In our .htaccess file, we can add our own specifications, which will override the defaults. The syntax is as follows:

AddType media_type .extension

Where media_type is one of the defined media types and .extension is the file name extension of the files you want to modify the content-type on. For example, if we wanted MP3 files to be downloaded instead of played in the browser:

AddType application/octet-stream .mp3

This would change the content-type of MP3 files (within the directory scope* of the .htaccess file) to application/octet-stream (arbitrary binary data – browser will offer to download) instead of the default of audio/mpeg.

If you want to specify multiple extensions to be served with the same content-type, you can use a space separated list in the regular line. If we want to apply this to a single file, we can use a files section and use ForceType instead of AddType:


ForceType application/octet-stream

In future articles, we’ll be further talking about the uses of .htaccess files.

* – The directory scope of a .htaccess file refers to where its contents will be applied. The file will have an effect on the files in the same directory as well as files in (infinitely) nested directories. In these nested directories, the “root” .htaccess file can be overridden with a new .htaccess in that nested directory.

1 Comment to “Moving Beyond SSH: Changing Content-Types”

  • If tried this before. If I change the output method in the emailstyle.xsl to “text” an error occurred:

    The value of the ‘method attribute may not be ‘Text’. :-(

    I like to by Feedreader Connect but if I canĀ“t send Mails to Lotus Notes I can’t use the tool. :-(

    PS: Is the emailstyle.xsl also used for save documents as HTML?