A common situation in the mobile web world is content
delivery. Java applications, widgets, music, video, wallpapers, and any
other content can be delivered to compatible devices, but this requires a
bit of explanation and expertise.1. Defining MIME Types
MIME types, are a key element for
content delivery. Many mobile browsers don’t care about the file
extension; they decide whether or not to accept the content based on the
MIME type delivered by the server. Remember that the MIME type travels
with the HTTP header response.
1.1. Static definition
The simplest way to define the right MIME types is to statically
define them on your web server. If you are working with a shared
hosting service, the control panels often allow you to define document
MIME types. If you manage your own server, you can set them up with
the following instructions.
1.1.1. Apache
In Apache, the simplest way is to open the mime.types file located in the conf folder of the Apache root. In your
favorite text editor, you can add one row per MIME type to be
configured.
You will find hundreds of MIME type declarations. The first
thing to do is to look for the following line and change the MIME
type to the correct one for mobile XHTML documents:
text/html html htm
As you can see, each line contains a MIME type followed by a
series of spaces or tabs and a space-separated list of file
extensions.
Warning:
This technique applies these changes to all the websites on
the server. If you want to make changes to only one website or one
folder of a website, you should create or edit the .htaccess file in the appropriate
folder and use the AddType
procedure:
AddType text/x-vcard vcf
You can find an Apache configuration file to download and use
with all the important mobile web MIME types at http://www.mobilexweb.com/go/mime.
1.1.2. Internet Information Server
Configuring MIME types in Microsoft IIS can be done
via the UI (as opposed to in Apache, where you need to edit a text
file). To configure the MIME types in IIS 6.0 on Windows XP or
Windows Server 2003:
Right-click the whole server, a website, or a folder and
select Properties.
On the HTTP Headers tab, click MIME Types.
Click New, type the file extension and MIME type, and
press OK to finish.
In IIS 7.0 for Windows Vista/7 and Windows 2008 Server:
Navigate to the level you want to manage.
In Features View, double-click on MIME Types.
In the Actions pane, click Add.
Type the file extension and MIME type and press OK to
finish.
Note:
In IIS, you can also manage MIME types from the command
line. Check the documentation for more information.
In IIS 7.0, it is also possible to define static MIME type
declarations in the web.config
file in your ASP.NET folder. The syntax is:
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
<mimeMap fileExtension=".vcf" mimeType="text/x-vcard" />
</staticContent>
</system.webServer>
</configuration
1.2. Dynamic definition
The other possible way to declare MIME types is to use
dynamic header declarations in your server script
code.
In PHP, you should define the MIME type before any other output
using the header function:
header('Content-Type: application/xhtml+xml');
If you are delivering downloadable content (not markup), like a
video, you should also define a filename. If not, when the file is
saved it will have a .php
extension and it will not work.
Note:
Remember that it is better to serve XHTML MIME types to mobile
websites. You can define them either statically or, if you are using
a server-side language, dynamically. You can also query WURFL or
another library to check what the preferred MIME type to deliver is
and use it to define the header.
To define the name of the file we use the Content-disposition header, as shown in the
following sample:
$path = '/videos/video.mp4';
header('Content-Type: video/mp4');
header('Content-disposition: attachment; filename=video.mp4');
// We serve the file from our local filesystem
header("Content-Length: " . filesize($path) );
readfile($path);
ASP.NET has a Response.ContentType property that we can
define:
// This is C# code
Response.ContentType = "application/xhtml+xml";
The filename should also be defined if it is downloadable
content, using Response.AddHeader.
In a Java servlet or JSP, you should define the headers using
the setContentType method of the
response object:
response.setContentType("application/xhtml+xml");
Note:
When you are serving non-markup content using a dynamic
script, if an error occurs you will not see the error details and
the content will be broken (imagine a JPEG with a PHP error as the
contents). You should capture any error, send yourself an email or
log the error details, and replace the output with generic
content.