title: How to detect the MIME type of files and serve it accordingly using Apache?
body: <p>Users can upload images to my site and name them (usually without .png or .jpg extension). This works when the image is served by an IMG tag. Apparently no MIME-type is necessary. The browsers can handle it because they <em>know</em> it has to be an image. When viewing the image directly, however, Apache serves the image as <code>text/plain</code>.</p>
  
  <p>Now, I can use mod_rewrite to serve an appropriate MIME type, but how to write a rule to determine the MIME type of a file?</p>
  
  <pre><code>RewriteEngine on
  RewriteCond something something involving grep, file, or whatever?
  RewriteRule ^.* - [last,type=image/png]
  </code></pre>
  
  <p>My current solution which occasionally serves the wrong MIME type (with no harm done, apparently):</p>
  
  <pre><code>RewriteEngine on
  
  # do nothing with .png, .jpg, .svgz, etc files                              
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule .*\.....? - [last]
  
  # add an image MIME type in order to force something other than text/plain  
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule .* - [last,type=image/png]
  </code></pre>