Adding IE 9 MIME Types to IIS 7

by Randall DuBois

The preview release of Microsoft Internet Explorer (IE) version 9 includes support for additional MIME Types for rich media applications. To support the additional MIME Types, the MIME types must be added to your IIS server.

The following additional MIME types are supported in IE 9:

File name extension MIME type
.svg image/svg+xml
.xht application/xhtml+xml
.xhtml application/xhtml+xml

To add these MIME types to your IIS server, you can:

Using the AddMimeTypes Script

You can also use the script included in this article to add the MIME Types on versions of Windows including Windows 2000 and later. The script uses WMI, and will add the MIME types on either a local or remote server.

To add MIME types for IE 9 to your IIS server using the AddMimeTypes script:

  1. Create a new script file using your authoring tool or a text editor.

  2. Copy the contents of the script file from the following section.

  3. Paste the contents of the script into your new script file.

  4. Save the script file as AddMimeTypes.vbs.

  5. At a Command Prompt, type the following command:

    cscript AddMimeTypes.vbs [RemoteServerName]
    

    Replace [RemoteServerName]> with the name of the remote computer you want to add the MIME types to.

    Note

    If the folder in which the script file is saved is not included in your Path environment variable, you must specify the full path to the script in the command, or change directories to the folder before running the command.

  6. Verify the MIME types were added either by using the IIS Manager.

The AddMimeTypes.vbs Script

'  File: AddMimeTypes2.vbs 
'  Copyright Microsoft Corp. 2010 
' 
'  This script adds the following Mime types to the root of a local or remote 
'  server. 
' 
'    (0) .svg    image/svg+xml
'    (1) .xht    application/xhtml+xml
'    (2) .xhtml  application/xhtml+xml
'
'  Usage: 
'      cscript AddMimeTypes.vbs [RemoteServerName]
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
OPTION EXPLICIT
DIM strServer, objWMIService, objOperatingSystem, colOperatingSystems
DIM osVersion, iisVersion, objMimeMap, objMimeTypes, Mime, i 
DIM objShell, strInetsrvDir, strInetpubScriptsDir
DIM numMimeTypes, numExistingMimes, mimeExists(2)
CONST ADS_PROPERTY_UPDATE = 2
IF WScript.Arguments.Length = 1 THEN
    strServer = WScript.Arguments( 0 )
ELSE
    strServer = "."
END IF
iisVersion = "0"
' What version of Windows and IIS am I using? If I am on IIS 5.x/6 use adsutil.vbs
' If I am on IIS 7 and later use appcmd
SET objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}" _
    + "!\\" + strServer + "\root\cimv2")
SET colOperatingSystems = _
    objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
FOR EACH objOperatingSystem IN colOperatingSystems
    osVersion = objOperatingSystem.Version
    strInetsrvDir = objOperatingSystem.SystemDirectory + "\inetsrv"
    strInetpubScriptsDir = objOperatingSystem.SystemDrive _
        + "\inetpub\AdminScripts"
NEXT    
IF ( 0 = Strcomp(strServer,".") ) THEN
    strServer = "localhost"
END IF
i = InStr(osVersion,"6.")
IF ( i = 1 ) THEN 
    iisVersion = "7"
END IF
i = InStr(osVersion,"5.")    
IF ( i = 1 ) THEN 
    iisVersion = "5"
END IF
i = InStr(osVersion,"5.2")    
IF ( i = 1 ) THEN 
    iisVersion = "6"
END IF
SET objShell = WScript.CreateObject("WScript.Shell")
SELECT CASE iisVersion
    CASE "7" 
        ' Run appcmd
        objShell.Run(strInetsrvDir + _
        "\appcmd.exe set config -section:system.webServer/staticContent /+_""[fileExtension='.svg',mimeType='image/svg%2Bxml']""")
        objShell.Run(strInetsrvDir + _
        "\appcmd.exe set config -section:system.webServer/staticContent /+""[fileExtension='.xht',mimeType='application/xhtml%2Bxml']""")
        objShell.Run(strInetsrvDir + _
        "\appcmd.exe set config -section:system.webServer/staticContent /+""[fileExtension='.xhtml',mimeType='application/xhtml%2Bxml']""")
        
    CASE "6", "5"
        ' Use ADSI to add the MIME types. 
        ' Can't use adsutil because they will destructively overwrite instead of appending
        SET objMimeMap = GetObject( "IIS://" + strServer + "/MimeMap" )
        objMimeTypes = objMimeMap.GetEx("MimeMap")
        ' Check to see if the MIME types already exist
        mimeExists(0)=0
        mimeExists(1)=0
        mimeExists(2)=0
        numExistingMimes=0
        CheckIfMimeTypeExists(objMimeMap)
        numExistingMimes=mimeExists(0)+mimeExists(1)+mimeExists(2)
        IF ( 3 = numExistingMimes ) THEN
            WScript.Echo("All extensions currently have mime types registered. Complete.")
            WScript.Quit
        END IF
        ' For extensions that don't have registered MIME types, add them
        numMimeTypes = UBound(objMimeTypes)
        REDIM PRESERVE objMimeTypes(numMimeTypes+3-numExistingMimes)
        i = numMimeTypes+1
        IF ( 0 = mimeExists(0) ) THEN
            SET objMimeTypes(i) = CreateObject("MimeMap")
            objMimeTypes(i).Extension = ".svg"
            objMimeTypes(i).MimeType = "image/svg+xml"
            i = i+1
        ELSE
            WScript.Echo("Mime type for .svg already exists")
        END IF
        IF ( 0 = mimeExists(1) ) THEN
            SET objMimeTypes(i) = CreateObject("MimeMap")
            objMimeTypes(i).Extension = ".xht"
            objMimeTypes(i).MimeType = "application/xhtml+xml"
            i=i+1
        ELSE
            WScript.Echo("Mime type for .xht already exists")
        END IF    
        IF ( 0 = mimeExists(2) ) THEN
            SET objMimeTypes(i) = CreateObject("MimeMap")
            objMimeTypes(i).Extension = ".xhtml"
            objMimeTypes(i).MimeType = "application/xhtml+xml"
        ELSE
            WScript.Echo("Mime type for .xhtml already exists")    
        END IF
            
        objMimeMap.PutEx ADS_PROPERTY_UPDATE, "MimeMap", objMimeTypes 
        objMimeMap.SetInfo()
    CASE ELSE
        WScript.Echo("Unsupported IIS version")
END SELECT

SUB CheckIfMimeTypeExists(MimeMap)
    objMimeTypes = MimeMap.GetEx("MimeMap")
    FOR EACH Mime IN objMimeTypes
        SELECT CASE LCase(Mime.Extension)
            CASE ".svg"
                mimeExists(0) = 1
            CASE ".xht"
                mimeExists(1) = 1
            CASE ".xhtml"
                mimeExists(2) = 1
        END SELECT
    NEXT  
END SUB