Forum Moderators: phranque

Message Too Old, No Replies

MIME type handling in .htaccess

         

Asia_Expat

9:04 am on Aug 7, 2007 (gmt 0)

10+ Year Member



I'm in the process of learning to convert my sites MIME type to be fully compliant.
I'm going to write all new documents with the .xhtml extension. However, in a test page I have created, php includes stopped working.

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?

Asia_Expat

9:08 am on Aug 7, 2007 (gmt 0)

10+ Year Member



OH!... now this is making the MIME type text/html, even thought it's application/xhtml+xml in the document! :(

[edited by: Asia_Expat at 9:09 am (utc) on Aug. 7, 2007]

Asia_Expat

12:22 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Hmmm... I guess forcing .xhtml docs to parse php includes is forcing the MIME type to become text/html
Any idea how to do includes in .xhtml and still keep the MIME type compliant?

jdMorgan

12:51 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I have my server set up to parse php in .html pages via htaccess

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

Asia_Expat

1:11 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



I'm using AddType x-mapp-php4 .html .htm .xhtml to tell it to parse php in xhtml docs but the MIME type in the header is coming out as text/html even though it's the proper xhtml MIME in the document head.

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.

Asia_Expat

1:27 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



I'm now using...

AddType application/xhtml+xml .xhtml
Addhandler application/x-httpd-php .html .htm .xhtml

... but still get the conflict between doc type and mime type in w3 validator.

jdMorgan

1:48 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you modified your PHP scripts to output the correct MIME-type header?

Jim

Asia_Expat

1:58 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



I'm not sure what you mean (and I guess that means no (and I guess that also means I'm clueless))

Asia_Expat

3:23 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



OK, I found this method on the web.
First, I created a mimetype.php file in the website root. In that file, I added...

<?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).

Asia_Expat

3:25 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Sorry... style tags didn't work :(