Chez Xavier

Home / Symfony : fixing the mime type for Internet Explorer

piaggio.jpg

An old Piaggio motorcycle in the streets of Paris, by night... - Flickr

Internet Explorer is the bad. Even if it is said to support the mime type */* since long, it indeed never supported application/xhtml+xml, the recommended mime-type for xhtml pages. When trying to display pages with this mime-type in IE8 or previous editions, Internet Explorer just proposes to download the page - needless to say that it will prevent some of your visitors to see the wonderful content of your pages.

Therefore, in order to get displayed in these browsers, web standards compliant websites must use the more common text/html mime-type. Indeed, the best what a developer can do is to serve the pages as application/xhtml+xml in all browsers except in those that don't accept it. Here is a simple filter for achieving this job in Symfony applications:


<?php

class nonApplicationXhtmlXmlCapableBrowsersFilter extends sfFilter
{
  public function execute($filterChain)
  {
    // execute next filter
    $filterChain->execute();
    
    // execute this filter only once
    if ($this->isFirstCall())
    {
      $content_type = sfContext::getInstance()->getResponse()->getContentType();

      if ((false !== stripos($content_type, 'application/xhtml+xml'))
          && (false === stripos($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml')))
      {
        sfContext::getInstance()->getResponse()->setContentType('text/html; charset='.sfConfig::get('sf_charset'));
      }
    }
  }
}

Put the code somewhere in your project, clear the cache and just activate the filter in the apps/APPLICATION_NAME/config/filters.yml:


rendering: ~
security:  ~

# insert your own filters here
nonApplicationXhtmlXmlCapableBrowsersFilter:
  class:      nonApplicationXhtmlXmlCapableBrowsersFilter

cache:     ~
common:    ~
execution: ~

In conclusion :

  • use this filter in order to display your web pages under Internet Explorer also,
  • then, explain your visitors that they should rather use or better web browser.