【问题标题】:Force video/webm mime type using .htaccess based on request uri根据请求 uri 使用 .htaccess 强制视频/webm mime 类型
【发布时间】:2011-05-29 16:46:32
【问题描述】:

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

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

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

如何使用包含上述规则的 .htaccess 将 mime 类型强制为:video/webm

我已经尝试过在 .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?

【问题讨论】:

  • 您提供的是真实文件还是虚拟 URL?
  • .fid 文件是真实文件,而我重写 url 以包含 myfile.webm

标签: apache .htaccess mod-rewrite mime-types webm


【解决方案1】:

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

RewriteRule someRegEx$ - [T=video/webm]

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

【讨论】:

  • 谢谢,我试了一下。但是,似乎有些东西在某处覆盖了这个 .htaccess 指令?除非我的语法关闭..
  • mod_rewrite 真的开启了吗?
【解决方案2】:

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

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

【讨论】:

  • 这不起作用,因为物理文件是 .fid 文件,而不是 .webm - .webm 只能通过重写添加
  • 感谢这对我有用!虽然它没有回答这个问题。
【解决方案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

【讨论】:

  • 我没有通过脚本发送文件。这些文件是文件以 .fid 结尾的实际文件(并且包含多种数据类型)
  • 如果您仔细阅读了我的帖子,您应该已经注意到:“如果我这样做:AddType video/webm .fid 发送了正确的 mime 类型 - 但这会破坏存储在.fid。使用 .fid 是设计要求,不能更改。”
  • @Jon 您有机会尝试 mod_headers 解决方案吗?
  • 您的建议导致了相同的text/plain 标头
【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-23
    • 2011-10-18
    • 2012-02-29
    • 2012-06-05
    • 2013-06-22
    • 2023-04-09
    • 2010-11-22
    • 2021-11-20
    相关资源
    最近更新 更多