Delivering HTML or XHTML depending on client Accept: header (Apache)

1 view
Skip to first unread message

Alan J. Flavell

unread,
Jan 19, 2002, 10:18:30 AM1/19/02
to

Recently there was some discussion on the relevant de.* group, in
which a useful mod_rewrite recipe came up for delivering the same
source document as HTML or XHTML depending on the client's Accept
header. It seems to me that this bit of ingenuity deserves a wider
audience. The recipe is near the end of this page

http://schneegans.de/tips/apache-xhtml.html

under the subheading "mod_rewrite". The descriptive text is in
German, but the recipe itself should be clear enough.

As the author says, there is one little problem with this in the form
offered: if some client were to try to explicitly refuse the xhtml
format by sending application/xml+xhtml;q=0 then the code would
misinterpret it as a positive request rather than as a refusal; I
guess a bit of work with regular expressions could correct that.

Also if a branch of the filetree contained documents in several
different character codings, it would need a bit of work with the
charset= setting.

Nevertheless, it has the benefit of needing only one static copy of
the source and not serving out the same content under different URLs,
as e.g symlinking the same source as .html and .xhtml versions would
involve.

The result (AFAICS) gets sent out with an appropriate 'Vary: accept'
header, and enough freshness validator data to be eligible for
cacheing (in proxy servers etc.), which is nice.

Sure, some would say this is based on a too simplistic underlying
approach - they would prefer to create a genuine XML-based source, and
deliver it, when necessary as HTML, by means of a transform process.
I'm just saying that if the simpler approach _is_ found acceptable,
then this technique seems a nifty way of applying it. Kudos to the
author, Christoph Schneegans (nice pages, too).

all the best

Henri Sivonen

unread,
Feb 27, 2002, 9:59:11 AM2/27/02
to
In article
<Pine.LNX.4.40.020119...@lxplus034.cern.ch>,

"Alan J. Flavell" <fla...@mail.cern.ch> wrote:

> Recently there was some discussion on the relevant de.* group, in
> which a useful mod_rewrite recipe came up for delivering the same
> source document as HTML or XHTML depending on the client's Accept
> header. It seems to me that this bit of ingenuity deserves a wider
> audience. The recipe is near the end of this page
>
> http://schneegans.de/tips/apache-xhtml.html

Those who want to do the trick and aren't permitted to enable
mod_rewrite but can use .var maps might be interested in this script I
posted in a Mozilla-related NG in November:

----8<----
#!/usr/bin/perl -w

@files = `ls -1`;
foreach(@files) {
chomp;
if(/^(.*)\.xml$/) {
$stem = $1;
symlink("$stem.xml", "$stem.html");
open(OUT, "> $stem.var")
or die ("Can't open $stem.var for output, $!\n");
print OUT "URI: $stem\n\n";
print OUT "URI: $stem.xml\n";
print OUT "Content-type: text/xml;q=0.99\n\n";
print OUT "URI: $stem.html\n";
print OUT "Content-type: text/html;q=1\n";
close(OUT);
chmod(0644, "$stem.var");
}
}
----8<----

--
Henri Sivonen
hsiv...@niksula.hut.fi
http://www.hut.fi/u/hsivonen/

Reply all
Reply to author
Forward
0 new messages