Force video/webm mime type using .htaccess based o

2019-04-21 23:51发布

问题:

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?

回答1:

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



回答2:

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>


回答3:

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


回答4:

Unable to get this to work in Apache, I gave up and moved to nginx instead. I got it to work in nginx using:

location ~\.webm$ {
  add_header Content-Type video/webm;
  rewrite  (.+?\.fid)/ /$1  break;
}