Force video/webm mime type using .htaccess based on request uri

Jon Skarpeteig picture Jon Skarpeteig · May 29, 2011 · Viewed 18.8k times · Source

I have a rewrite rule in .htaccess

RewriteRule   (.+?\.fid)/ /$1 [L]

with a request URI like: /123.fid/myfile.webm

How can I force the mime type to: video/webm using .htaccess including the rule above?

What I have attempted already, to add on TOP of the .htaccess file without success:

AddType video/webm .webm

and

<FilesMatch  "\.webm$">
  ForceType video/webm
</FilesMatch>

I use apaches mime_magic module to look up mime type of .fid files, but this doesn't apply to webm files. I'm assuming it's the RewriteRule which is causing problems with the file type, and I need to look for webm in the request uri somehow.

If I do: AddType video/webm .fid the correct mime type is sent - but this breaks any other file format stored in .fid. Using .fid is a design requirement and cannot change.

*Edit:

I also attempted:

RewriteCond %{REQUEST_URI} \.webm$
RewriteRule .* - [T=video/webm]

and

RewriteRule \.webm$ - [T=video/webm]

with the same result. The mime type served is text/plain. Could this be mime_magic module interfiering?

I also attempted to add: DefaultType video/webm which works. This is the closest thing to a solution currently, as mime_magic module seems to find the correct mime types to send, but I don't find this to be a particularly elegant solution

*Edit2: AddType video/webm .fid IS working - how can I conditionally do AddType based on request uri?

Answer

Floern picture Floern · May 29, 2011

You can use the T-flag in a RewriteRule:

RewriteRule someRegEx$ - [T=video/webm]

http://httpd.apache.org/docs/current/rewrite/flags.html#flag_t