Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
HOWTO: Configure Apache to Serve XHTML Correctly
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
nihilo
Apprentice
Apprentice


Joined: 05 Nov 2002
Posts: 168
Location: berkeley, ca, usa

PostPosted: Tue Sep 25, 2007 4:46 am    Post subject: HOWTO: Configure Apache to Serve XHTML Correctly Reply with quote

I wrote a how-to that explains how to configure Apache to correctly serve XHTML documents and how to handle Internet Explorer, which chokes on XHTML if it has the correct mime-type. I don't know how many other Gentoo users use XHTML, but I thought it might be useful to somebody, so I'll briefly summarize the steps here.

For more details, please see the expanded version, and if you don't know what XHTML is or you aren't absolutely clear on the differences between XHTML and HTML, I'd advise you not to make any changes. This is intended for people who are trying to use or are already using XHTML but perhaps are having trouble getting everything working (or maybe you're always serving it as text/html at present, which is definitely A Bad Thing (TM)). There is a lot more of a difference between HTML and XHTML than just adding a slash on br elements and other elements that have no content.

My Apache version is 2.2.6, but the instructions should very close if not identical for other versions.

The process, in brief, involves registering .xhtml files with the mime-type of application/xhtml+xml, using the .xhtml extension for XHTML files and .html or something else for non-XHTML files, and then configuring mod_rewrite to change the mime-type for clients that declare they can't accept the above mime-type or that are internet explorer.

All the Apache file changes I made are in /etc/apache2/vhosts.d/00_default_vhost.conf, but if you're not using virtual hosts, you'll want to improvise.


    * Add the following directive to the configuration, at the top level:
    Code:
    AddType application/xhtml+xml;q=0.8 .xhtml

    * Add the following directive just below:
    Code:
    DirectoryIndex index.xhtml index.html

    * Add the following rewrite rules:
    Code:
    <IfModule mod_rewrite.c>
      RewriteEngine on
      # Uncomment RewriteBase line if adding inside per-directory
      # configuration files (e.g., .htaccess):
      # RewriteBase /
      RewriteCond %{REQUEST_URI} \.xhtml$
      RewriteCond %{HTTP_USER_AGENT} MSIE [OR]
      RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml\s*;\s*q=0\.?0*(\s|,|$)
      RewriteRule .* - [T=text/html]
    </IfModule>

    * Convert the file extension of all files that need to be served using the new XHTML mime-type. If that is all the files, you might do something like the following one-liner:
    Code:
    for f in $(find base_html_dir/ -type f -name '*.html');
    do
        mv -i "$f" "$(dirname $f)/$(basename $f .html).xhtml";
    done

    * Make sure you didn't hose your Apache configs and that you can still reboot:
    Code:
    /etc/init.d/apache2 configtest

    * Reboot Apache and verify using cUrl or wget that things behave correctly.


What you should see is that all files that have .xhtml extension should now be served as application/xhtml+xml unless the client is Internet Explorer or the client explicitly states in the Accept request header that it doesn't accept files of that type.

And that's it.

Any questions?

[edit] Combined 2 regexes into 1 regex in rewrite conditions.
Back to top
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Sun Sep 30, 2007 12:20 pm    Post subject: Reply with quote

thanks! I've jumped from one article to an other starting with your link to the whole article, This is really interesting, and I'm going the try serving xhtml correctly too :)
_________________
The End of the Internet!
Back to top
View user's profile Send private message
nihilo
Apprentice
Apprentice


Joined: 05 Nov 2002
Posts: 168
Location: berkeley, ca, usa

PostPosted: Mon Oct 01, 2007 9:41 pm    Post subject: Reply with quote

Thanks for the feedback, truc. Let me know if you get things working correctly and if anything was unclear in my notes.
Back to top
View user's profile Send private message
yngwin
Developer
Developer


Joined: 19 Dec 2002
Posts: 4563
Location: Suzhou, China

PostPosted: Tue Oct 02, 2007 12:06 am    Post subject: Reply with quote

One of my old rants may be interesting for people to consider:
http://berkano.net/bits/2005/06/15/markup-in-the-real-world
_________________
"Those who deny freedom to others deserve it not for themselves." - Abraham Lincoln
Free Culture | Defective by Design | EFF
Back to top
View user's profile Send private message
transient
l33t
l33t


Joined: 13 Jan 2005
Posts: 759
Location: New Zealand

PostPosted: Tue Oct 02, 2007 10:52 am    Post subject: Reply with quote

It's nice to see someone who actually knows the evil of sending xhtml as text/html.
Sadly though, I've only seen one single website that's ever had a need to use xhtml, and that was a mathematical physics site that needed to use MathML in each page.
I won't go into a giant rant about the silliness of XHTML 1.x or 2.0 though here :P
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum