The Mobile Developer
by Eric Giguère
Enabling HDML/WML Support in Apache
In a
previous
column I talked about the complications that WAP and related
technologies (like HDML) are going to add to your life as a
website designer/developer. Short of going off and rebuilding your
website using XML (see
Cocoon, part
of the Apache XML Project, for one way to do it), how are you going
to add even minimal support for cellphones to your website? Let me
show you a few ideas that work with the Apache web server.
The first thing to do, of course, is register the appropriate MIME
types with your web server. The registration associates a given
MIME type (used by the browser to identify the kind of content
the web server is sending it) with one or more file extensions.
For Apache, add these lines to your httpd.conf file (if you have
root access to the web server) or to your .htaccess file:
AddType text/vnd.wap.wml .wml
AddType text/x-hdml .hdml
Notice how we've added support for both WML and HDML. Although HDML
is being superceded by WML, in North America most of the browsing
cellphones do not yet support WML, so whenever possible try to
support both. Now you can try placing a few simple WML/HDML files
on your website and you should be able to access them from a
microbrowser.
What if you don't yet have any interesting WML/HDML content to
put on your website but you want to let users know that it's
coming, maybe by redirecting them to a precanned "under
construction" message? Ideally you would do this no matter
what URL is being accessed. This is actually fairly simple to do
using Apache's
mod_rewrite
module. Using mod_rewrite you can add directives to your httpd.conf
or .htaccess file that redirect requests based on the browser type and/or
the list of MIME types the browser accepts. Say you want to redirect
all requests from browsers that accept the text/x-hdml MIME type to
a servlet. Try the following:
RewriteEngine on
RewriteBase / # only needed in .htaccess file
# First rule
RewriteCond %{REQUEST_FILENAME} ^/servlet
RewriteRule ^(.+) - [PT]
# Second rule
RewriteCond %{HTTP_ACCEPT} text/x-hdml
RewriteRule ^/(.*) /servlet/HandleHDMLRequests$1 [PT]
The first rule matches all URLs that start with "/servlet" and passes
them through unchanged. The next rule redirects all HDML requests
to a servlet called HandleHDMLRequests. The original URL path
is appended to the servlet request path and is thereby available
within the servlet, making it easy for you to tailor responses using
the same servlet.
If you don't want to redirect requests to a servlet (or some other
dynamic mechanism -- Perl scripts would work just as well), you can
return a precanned file instead:
RewriteEngine on
RewriteBase / # only needed in .htaccess file
# Pass .hdml requests through unchanged
RewriteRule ^(.*).hdml$ $1.hdml [L]
# All HDML requests go to index.hdml
RewriteCond %{HTTP_ACCEPT} text/x-hdml
RewriteRule ^(.*)$ /index.hdml [PT]
Now all requests for HDML content return /index.hdml.
Don't forget to add WML equivalents for any redirections
you do, which I've left out to keep things brief. Put
the WML rules ahead of the HDML rules so that browsers
that support both will get WML. And before you do any
of this, read the excellent
URL
Rewriting Guide for a good understanding of how Apache
rewriting works.
Eric Giguère is the author of
Palm Database Programming: The Complete Developer's Guide
and an upcoming book on the Java 2 Micro Edition. He works
as a developer in Sybase's Mobile & Embedded Computing division.
Visit his website at www.ericgiguere.com
or send him mail at ericgiguere@ericgiguere.com.