How to Serve Unknown File Types in IIS 7: A Complete Guide for Static Content


2 views

When working with IIS 7, you might encounter situations where you need to serve files with unknown or uncommon extensions. By default, IIS only serves files with registered MIME types, returning a 404 error for unrecognized extensions. This becomes particularly frustrating when you're managing a directory meant solely for static content.

The most straightforward approach is to configure IIS to use the StaticFile handler for all files in your target directory. Here's how to implement this:

<configuration>
  <system.webServer>
    <handlers>
      <add name="StaticFile" path="*" verb="*" 
           modules="StaticFileModule" 
           resourceType="Either" 
           requireAccess="Read" />
    </handlers>
  </system.webServer>
</configuration>

Another approach is to define a wildcard MIME type that catches all file extensions:

<configuration>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".*" mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>
</configuration>

When implementing this solution, consider these security best practices:

  • Apply the configuration only to specific directories
  • Disable script execution for the target directory
  • Set appropriate file system permissions
  • Consider adding request filtering for dangerous extensions

After implementing the solution, test with various file types:

# PowerShell test script
$files = @("test.xyz", "data.unknown", "file.custom")
foreach ($file in $files) {
    $uri = "http://yoursite.com/static-files/$file"
    try {
        $response = Invoke-WebRequest -Uri $uri -Method Head
        Write-Host "$file : $($response.StatusCode)"
    } catch {
        Write-Host "$file : Failed ($($_.Exception.Message))"
    }
}

While this solution works well for static content directories, be aware that:

  • IIS must examine each file's extension
  • No content-type optimization is performed
  • Browser handling of unknown types may vary

When working with IIS 7, you might encounter situations where you need to serve files with unknown or unregistered MIME types. By default, IIS blocks such requests unless the file extension is explicitly added to the MIME types list. This becomes cumbersome when dealing with dynamic content or multiple file types in a specific directory.

To serve all files in a directory as static content (without execution), follow these steps:


<configuration>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".*" mimeType="application/octet-stream" />
    </staticContent>
    <handlers>
      <remove name="StaticFile" />
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" 
           resourceType="Either" requireAccess="Read" />
    </handlers>
  </system.webServer>
</configuration>

For a specific directory where you want to serve all files as static content:

  1. Create a web.config file in the target directory
  2. Add the following configuration:

<configuration>
  <system.webServer>
    <handlers>
      <clear />
      <add name="StaticFile" path="*" verb="*" 
           modules="StaticFileModule" resourceType="Either" 
           requireAccess="Read" />
    </handlers>
    <staticContent>
      <mimeMap fileExtension="*" mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>
</configuration>

When implementing this solution:

  • Ensure the directory has execution permissions disabled
  • Restrict write access to prevent unauthorized uploads
  • Consider adding specific file extensions to block potentially dangerous types

After applying these changes, test with various file types:


curl -I http://yourserver/path/to/unknown/file.xyz

You should receive a 200 OK response with the file served as application/octet-stream.

For more control, you can use URL Rewrite module:


<rule name="Serve Unknown Files" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" />
  </conditions>
  <action type="Rewrite" url="{R:0}" />
</rule>