根据请求uri使用.htaccess强制视频/ webm mime类型

时间:2021-10-24 11:18:19

I have a rewrite rule in .htaccess

我在.htaccess中有一个重写规则

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

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

请求URI如:/123.fid/myfile.webm

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

如何使用.htaccess强制mime类型:video / webm,包括上面的规则?

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

我已经尝试过,添加.htaccess文件的TOP但没有成功:

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.

我使用apaches mime_magic模块查找mime类型的.fid文件,但这不适用于webm文件。我假设它是RewriteRule导致文件类型出现问题,我需要以某种方式在请求uri中查找webm。

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.

如果我这样做:AddType video / webm .fid发送正确的mime类型 - 但这会破坏存储在.fid中的任何其他文件格式。使用.fid是设计要求,不能更改。

*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?

结果相同。服务的mime类型是text / plain。这可能是mime_magic模块的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

我还尝试添加:DefaultType video / webm。这是目前最接近解决方案的东西,因为mime_magic模块似乎找到了要发送的正确mime类型,但我发现这不是一个特别优雅的解决方案

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

* Edit2:AddType video / webm .fid IS工作 - 如何根据请求uri有条件地执行AddType?

4 个解决方案

#1


6  

You can use the T-flag in a RewriteRule:

您可以在RewriteRule中使用T-flag:

RewriteRule someRegEx$ - [T=video/webm]

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

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

#2


6  

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

当我将以下行添加到我的.htaccess文件时,它对我有用:

<IfModule mod_rewrite.c>
  AddType video/ogg .ogv
  AddType video/webm .webm
  AddType video/mp4 .mp4
</IfModule>

#3


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):

如果您使用脚本(而不是真实文件)发送数据,则脚本必须发送正确的标头,例如使用PHP(在任何其他输出之前):

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


1  

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

无法让它在Apache中运行,我放弃了,转而使用nginx。我使用它在nginx中工作:

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

#1


6  

You can use the T-flag in a RewriteRule:

您可以在RewriteRule中使用T-flag:

RewriteRule someRegEx$ - [T=video/webm]

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

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

#2


6  

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

当我将以下行添加到我的.htaccess文件时,它对我有用:

<IfModule mod_rewrite.c>
  AddType video/ogg .ogv
  AddType video/webm .webm
  AddType video/mp4 .mp4
</IfModule>

#3


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):

如果您使用脚本(而不是真实文件)发送数据,则脚本必须发送正确的标头,例如使用PHP(在任何其他输出之前):

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


1  

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

无法让它在Apache中运行,我放弃了,转而使用nginx。我使用它在nginx中工作:

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