...

How to serve XHTML as application/xhtml+xml

brothercake
05-10-2003, 10:16 PM
This isn't a question, I'm just posting info for reference.

A big problem with serving XHTML as application/xhtml+xml is that IE doesn't understand the mime-type, and tries to download the page instead of displaying it. What's needed is a way of changing the mime-type conditionally - get the browser to tell us what it accepts.

Here are two ways:

1 - using mod_rewrite in an Apache .htaccess file:

# serve .xhtml as xml - this could equally be .html
AddType application/xhtml+xml xhtml

# serve as tag soup if necessary
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} (text/html|\*/\*)
RewriteCond %{REQUEST_FILENAME} .*\.xhtml
RewriteRule ^.*$ - "[T=text/html,L]"


2 - using PHP; this method is double-useful because you can also add the <?xml?> processing instruction if appropriate:

<?php
if (stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
header("Content-type: application/xhtml+xml");
echo ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
}
else { header("Content-type: text/html"); }
?>

liorean
05-10-2003, 10:22 PM
Is there a way to make that php be applied to all .htm, .html, .xhtml files that are requested instead of inserting it in the files and making sure they are parsed as php? Through a combinarion of .htaccess/mod_rewrite and php?

brothercake
05-10-2003, 11:11 PM
Well you can use ForceType to get .html and .xhtml to be parsed by PHP. Then I believe there's a way to set php.ini directives from within .htaccess, by which you can automatically include bits of code into every file that it parses. Not sure how to do that though.

Alex Vincent
05-11-2003, 09:20 PM
AFAIK, there are special problems with PHP and XHTML.

(1) The PHP script with the header() codes must come before any other characters in the document. This means your document cannot by default include the <?xml version="1.0" encoding="UTF-8"?> processing instruction (though you can include it in the PHP script's output). Religious zealots like myself hate leaving off the XML prolog.. ;)

(2) To pass the PHP-scripted document to the PHP interpreter, you need to have code like the following in your .htaccess file:

AddType application/x-httpd-php .pxhtml

Then, of course, you still need the content-type override, as PHP usually outputs HTML.

liorean: yes, running a PHP script as a directory within the URL can be done; Firepages showed me once about that. I'd have to look on my machine for the details.

brothercake
05-11-2003, 10:37 PM
Interesting ..

1 - the example I posted does just that - working from the basis that the documents themselves don't have the <?xml processing instruction - this is added in only if the document is really XML

2 - unless the document has the .php extension already

But in both these cases I am assuming a situation where you're already working in PHP whether for this purpose or not.

Alex Vincent
05-11-2003, 11:31 PM
I stickied this thread as informative for the XML group; if you have questions for PHP, they don't necessarily belong here. Better to post those questions in the PHP forum. (But feel free to PM or e-mail me a thread if you want to get my attention. :cool: )

As a moderator, I'm probably going to edit or delete posts in this thread that don't relate to XHTML or XML. I'll give it a few days before I do the pruning.

UPDATE: Pruning's done. Hope no one gets upset about it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum