Apache动态设置MIME类型

发布于 2025-02-13 05:24:48 字数 877 浏览 0 评论 0原文

我们有Mutiple文件,需要根据文件类型设置MIME类型。例如:

/{path}/alpha.woff --> font/woff
/{path}/beta.woff2 --> font/woff2
/{path}/gamma.ttf --> font/ttf

我们已经在Apache配置文件中添加了以下摘要,但不确定如何使用一个位置匹配属性实现

<LocationMatch "\.(?i:woff|woff2|ttf)$">
        Header set Content-Type font/{dynamically set woff OR woff2 OR ttf }
</LocationMatch>

这一目标可以使用多个位置Match(每个字体)完成,但看起来没有优化。 谁能建议更好的解决方案来应对这种情况?

用正则犯错的尝试。

 <LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))$">
   Header set Content-Type font/woff // Work fine but set font/woff for all fonts
 </LocationMatch>

投掷错误%

<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))$">
   Header set Content-Type font/%{env:MATCH_FONTTYPE}
 </LocationMatch>

以下在配置未识别的标题格式%的

We have mutiple files where we need to set MIME type based upon file type. Ex:

/{path}/alpha.woff --> font/woff
/{path}/beta.woff2 --> font/woff2
/{path}/gamma.ttf --> font/ttf

We have added the below snippet in apache config file but not certain how to achieve with one LocationMatch attribute

<LocationMatch "\.(?i:woff|woff2|ttf)
quot;>
        Header set Content-Type font/{dynamically set woff OR woff2 OR ttf }
</LocationMatch>

This can be done with multiple LocationMatch (one for each font) but looks not optimized one.
Can anyone suggest better solution to deal with such situation?

Tried with regex which throw an error.

 <LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))
quot;>
   Header set Content-Type font/woff // Work fine but set font/woff for all fonts
 </LocationMatch>

Below throw error in config

<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))
quot;>
   Header set Content-Type font/%{env:MATCH_FONTTYPE}
 </LocationMatch>

Unrecognized header format %

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-02-20 05:24:49

可以在conf/mime.types apache http服务器的配置文件中配置MIME类型httpd/httpd/trunk/docs/conf/mime.types“ rel =” nofollow noreferrer“>此处:

font/woff                                       woff
font/woff2                                      woff2
font/ttf                                        ttf

如果您无法更新此文件,并且location -match指令是您唯一的选项,然后从Apache 2.4.8开始,您可以使用名为Regex Capture组,请参见< a href =“ https://httpd.apache.org/docs/2.4/mod/core.html#locationmatch” rel =“ nofollow noreferrer“>文档。

在下面的示例中,我命名了捕获组fonttype,因此,按照会议,它将被放入环境变量match_fonttype。此环境变量可以在forcetype表达式中使用:

<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))$">
    ForceType font/%{env:MATCH_FONTTYPE}
</LocationMatch>

The mime types can be configured in the conf/mime.types configuration file of the Apache HTTP Server, see the example here:

font/woff                                       woff
font/woff2                                      woff2
font/ttf                                        ttf

If you can't update this file, and LocationMatch directive is your only option, then starting from Apache 2.4.8 you can use named regex capture groups, see documentation.

In the following example, I've named the capture group fonttype, so by convention it will be put into the environment variable MATCH_FONTTYPE. This environment variable can be used in the ForceType expression:

<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))
quot;>
    ForceType font/%{env:MATCH_FONTTYPE}
</LocationMatch>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文