9

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

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

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

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

我已经尝试过在 .htaccess 文件的顶部添加但没有成功:

AddType video/webm .webm

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

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

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

*编辑:

我也尝试过:

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

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

结果相同。提供的 mime 类型是text/plain. 这可能是 mime_magic 模块干扰吗?

我还试图补充:DefaultType video/webm哪个有效。这是目前最接近解决方案的方法,因为 mime_magic 模块似乎找到了要发送的正确 mime 类型,但我认为这不是一个特别优雅的解决方案

*Edit2AddType video/webm .fid正在工作 - 我如何根据请求 uri 有条件地执行 AddType?

4

4 回答 4

7

您可以在 RewriteRule 中使用 T 标志:

RewriteRule someRegEx$ - [T=video/webm]

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

于 2011-05-29T16:55:37.940 回答
6

当我在.htaccess文件中添加以下行时,它对我有用:

<IfModule mod_rewrite.c>
  AddType video/ogg .ogv
  AddType video/webm .webm
  AddType video/mp4 .mp4
</IfModule>
于 2011-11-16T00:35:27.127 回答
3

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

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

如果是真实文件,您可以使用内容协商(而不是重写)并且:

AddType video/webm .fid

编辑:

不幸的是,我不在 apache 附近,但这可能值得一试:

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

Header set Content-Type video/webm env=is_webm
于 2011-05-29T17:11:18.303 回答
1

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

location ~\.webm$ {
  add_header Content-Type video/webm;
  rewrite  (.+?\.fid)/ /$1  break;
}
于 2011-06-04T13:20:35.050 回答