Forum Moderators: phranque
I have my server set up to parse php in .html pages via htaccess and after a little fiddling around, I got it to work in .xhtml pages by simply adding it to the list of extensions, here...
AddType application/xhtml+xml .xhtml
AddType x-mapp-php4 .html .htm .xhtml
However, I learned that I had to put the x-mapp-php4 line after the other line or it wouldn't work (previously, the lines were reversed). Can someone tell me why that is the case?
Further, I have a managed server and if the host change to php5, will this all crash down around me?
How are you telling your server to parse for PHP in .htaccess? Note that only text/html files can be parsed using mod_include's XBitHack.
Also, be careful not to confuse AddHandler with AddType -- These do two very different things. A 'hack' used by PHP confuses them a little, but it's best to start out with correct AddHandler/AddType definitions, and then tweak from there.
The document type declared in the page headers means nothing on the Web; It's only used when your pages are saved and subsequently opened from the user's local hard drive.
Note that *only* XHTML pages should be served as XHTML+XML. Using this MIME-type brings with it a host of new requirements and problems (not the least of which is that IE doesn't support it properly), and you should not be using XHTML if you don't absolutely need it [webmasterworld.com] -- for example, for a WAP2.0 Mobile site. This is not something to do 'just because it's the latest thing' and in many cases, is a bit like serving PDF when HTML is requested; It is not "the latest and greatest version of HTML," it is essentially a different markup language.
Jim
I'm trying to do this because my doc types are all XHTML 1.1 and the MIME type cannot be text/html. I wish to make my site totally compliant in this regard and I will use some other tweaks to recognise IE6 and produce the old MIME type in such case.
I'm reading up on handlers now but it's doing my head in. I don't understand what I've done wrong.
<?php
$charset = "iso-8859-1";
$mime = "text/html";
function fix_code($buffer) {
return (str_replace(" />", ">", $buffer));
}
if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
# if there's a Q value for "application/xhtml+xml" then also
# retrieve the Q value for "text/html"
if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i",
$_SERVER["HTTP_ACCEPT"], $matches)) {
$xhtml_q = $matches[1];
if(preg_match("/text\/html;q=0(\.[1-9]+)/i",
$_SERVER["HTTP_ACCEPT"], $matches)) {
$html_q = $matches[1];
# if the Q value for XHTML is greater than or equal to that
# for HTML then use the "application/xhtml+xml" mimetype
if($xhtml_q >= $html_q) {
$mime = "application/xhtml+xml";
}
}
# if there was no Q value, then just use the
# "application/xhtml+xml" mimetype
} else {
$mime = "application/xhtml+xml";
}
}
# special check for the W3C_Validator
if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
$mime = "application/xhtml+xml";
}
# set the prolog_type according to the mime type which was determined
if($mime == "application/xhtml+xml") {
$prolog_type = "<?xml version='1.0' encoding='$charset'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>";
} else {
ob_start("fix_code");
$prolog_type = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>
<html lang='en'>";
}
# finally, output the mime type and prolog type
header("Content-Type: $mime;charset=$charset");
header("Vary: Accept");
print $prolog_type;
?>
The top of my document now looks like this...
<?php include($_SERVER['DOCUMENT_ROOT'] . "/mimetype.php");?>
<head>
<title>FooBar</title>
<meta name="description" content="A nice page about Foos and associated Bars" />
<meta name="keywords" content="foo, bar" />
<link rel="stylesheet" href="foobar.css" type="text/css" media="screen" />
</head>
Now it validates perfectly over a w3 validator, including the correct MIME type.
Here is the source from browser (Firefox)...
<?xml version='1.0' encoding='iso-8859-1'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<title>FooBar</title>
<meta name="description" content="A nice page about Foos and associated Bars" />
Here is the source from browser (Internet Explorer)...
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>
<html lang='en'>
<head>
<title>FooBar</title>
<meta name="description" content="A nice page about Foos and associated Bars" />
----------------------------------------
Could you please examine the above and tell me if you think everything looks to be in order?
I'm pretty sure it's working because javascript now fails to work in FF but still works in IE (so I guess that is disclosing non XHTML compliant javascript).