Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to serve XHTML as application/xhtml+xml

    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:
    Code:
    # 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 Code:
    <?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"); }
    ?>
    Last edited by brothercake; 05-10-2003 at 11:19 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    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.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by brothercake; 08-06-2003 at 03:06 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    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. )

    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.
    Last edited by Alex Vincent; 05-15-2003 at 02:00 AM.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •