Apache动态设置MIME类型
我们有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以在
conf/mime.types
apache http服务器的配置文件中配置MIME类型httpd/httpd/trunk/docs/conf/mime.types“ rel =” nofollow noreferrer“>此处:如果您无法更新此文件,并且
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
表达式中使用:The mime types can be configured in the
conf/mime.types
configuration file of the Apache HTTP Server, see the example here: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 variableMATCH_FONTTYPE
. This environment variable can be used in theForceType
expression: