Serving different index pages for WAP/WML and HTML clients from the same URL

By setting up <VirtualHost> definitions in the Apache configuration file, it is trivial to provide a 'www.' address for [HTML] web browsers and a 'wap.' address for phones and other WML devices.

However for improved usability, it would be better to have a single address which users can enter into any device. The server should then Do The Right Thing and send appropriate content. An extra usability consideration for wireless devices (e.g. WAP phones) is that URLs should be kept as short as possible. No-one likes typing long addresses on a telephone's keypad, so a URL like http://wap.jonallen.info would be better if it were shortened to just http://jonallen.info.

The HTTP_ACCEPT header provides quite an accurate way to determine if your site is being accessed via a WAP device. As part of this header, any WAP clients should send the string text/vnd.wap.wml.

A notable exception to this is Opera, which sends the WML content type if the URL is in the form http://wap.site.tld, but not for URLs like http://www.site.tld or http://site.tld, so if you are using this code then make sure your host name does not start http://wap. or Opera users will not be able to view the HTML version of your site.

Another point to note is that not all HTML clients send text/html in the header (such as MSIE). Therefore the system must default to HTML and send WML content only if a WAP browser has been identified, rather than doing it the other way round.

The code:

#! /usr/bin/perl
 
$ENV{HTTP_ACCEPT}=~/vnd\.wap\.(wml)/;
open FILE,"$ENV{DOCUMENT_ROOT}$ENV{REQUEST_URI}index.${\($1||'html')}" or die($!);
print "Content-type: text/${\($&||'html')}\n\n";
print while ();

So why does this open the file and not just send an HTTP Location: header?

Well, the first version did exactly that, but some Motorola phones just ignored the Location: header. Sending the data directly is also quicker - if an HTTP redirect is used, there are actually two requests happening which when using WAP over GSM (9600 baud) can produce a noticeable delay.

I have used the above method at http://jonallen.info, and it has been tested on the following platforms:

WAP phones:

  • Ericsson T39
  • Motorola T260
  • Nokia 6210
  • Panasonic GD93

Web browsers:

  • Lynx
  • Mozilla 0.9.3
  • MSIE 5
  • Netscape 4.77
  • Opera 5