Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 321
Jon Skarpeteig Avatar asked May 29 '11 16:05

Jon Skarpeteig


3 Answers

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

like image 183
Floern Avatar answered Sep 27 '22 20:09

Floern


It worked for me, when I added the following lines to my .htaccess file:

<IfModule mod_rewrite.c>
  AddType video/ogg .ogv
  AddType video/webm .webm
  AddType video/mp4 .mp4
</IfModule>
like image 45
taltu Avatar answered Sep 27 '22 19:09

taltu


If you are sending the data with a script (not real files) then the script must send the correct headers, for example with PHP (before any other output):

header("Content-type: video/webm");

In case of real files you can use content-negotiation (instead of rewrite) and:

AddType video/webm .fid

Edit:

Unfortunately I'm not near apache, but this might worth a try:

RewriteCond %{REQUEST_URI} \.webm$
RewriteRule (.*) $1 [E=is_webm:true]

Header set Content-Type video/webm env=is_webm
like image 27
vbence Avatar answered Sep 27 '22 18:09

vbence