Kyl*_*ett 8 fonts iis-7 web-config cors
有没有办法在IIS7上只为特定类型的文件启用CORS?我将关闭这篇文章(https://www.maxcdn.com/one/tutorial/how-to-use-cdn-with-webfonts/),并注意到Apache和nginx示例显示CORS仅在启用时启用请求正在查找某个Content-Type,而IIS示例显示为所有内容启用了CORS.
我有一个外部网站上引用的CSS,但浏览器阻止了字体文件,并希望在IIS7上启用CORS,但仅限于.woff,.woff2,.tff,.eot和.svg文件.如果可能的话,我不希望为所有内容启用CORS.
Der*_*ker 25
Hackerman的答案很棒,它让我找到了解决方案,然而,我必须做出一些调整.第一个是将规则放在节点outboundRules
下的部分中rewrite
.
<outboundRules>
<rule name="Enable CORS for Fonts">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="^[^\?]+\.(ttf|otf|eot|woff|woff2|svg)(\?.*)?$" />
</conditions>
<action type="Rewrite" value="*" />
</rule>
</outboundRules>
Run Code Online (Sandbox Code Playgroud)
最后,正则表达式已更新,以防止下面的请求,这将允许有人请求跨源的任何URL:
/some/critical/javascript/file.js?v=.woff
/api/secure/users?v=.woff
Run Code Online (Sandbox Code Playgroud)
......但它仍然允许以下内容
/some/font.woff
/some/font.woff?etag
/some/font.woff?v=123
Run Code Online (Sandbox Code Playgroud)
小智 13
您应该将此代码添加到Web.config:
<location path="fonts/FontName.ttf">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
Run Code Online (Sandbox Code Playgroud)
您可以安装 IIS 重写模块https://www.microsoft.com/en-us/download/details.aspx?id=7435。安装模块后,重新启动 IIS 管理器,然后单击 \xe2\x80\x9cSites\xe2\x80\x9d 节点下的网站。如果您在 IIS 部分右侧窗口中看到 URL 重写模块符号(如下所示),那么您就可以开始了:
\n\n\n\n然后在您的 Web.config 上,您需要添加以下规则:
\n\n<rewrite>\n <rules>\n <rule name="Add Cross Origin Access">\n <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />\n <conditions>\n <add input="{REQUEST_URI}" pattern="^[^\\?]+\\.(ttf|otf|eot|woff|woff2|svg)(\\?.*)?$" />\n </conditions>\n <action type="Rewrite" value="*"/>\n </rule>\n </rules>\n</rewrite>\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
7497 次 |
最近记录: |