Save the program into your cgi-bin directory (e.g. as redir.pl), then set your server to use it as the default index page. For Apache, the directive is:
DirectoryIndex /cgi-bin/redir.pl
Place an 'index.html' and 'index.wml' file in the document root directory, and you will then be able to use the same URL (http://yoursite) for both HTML and WML (WAP) clients.
#! /usr/bin/perl
print"Location: index.".(($ENV{HTTP_ACCEPT}=~/html/g)[0]||'wml')."\n\n
+";
Update:
Don't you just love browser compatibility issues? The Motorola browser doesn't like the
Location: header, and MSIE doesn't list
text/html in its HTTP_ACCEPT.
Amended code is shown below:
#! /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 (<FILE>);
Update (12-Oct-2001) How this works:
All WAP clients (which I have tried) will send the string
text/vnd.wap.wml as part of the HTTP_ACCEPT header, however not all HTML clients send
text/html (MSIE...!). Therefore this provides quite an accurate way to determine if your site is being accessed via a WAP phone.
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.
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.