Is there a way to enable CORS for only files of a certain type on IIS7? I am going off this article (https://www.maxcdn.com/one/tutorial/how-to-use-cdn-with-webfonts/) and noticed that the Apache and nginx examples show that CORS is only enabled if the request is looking for a certain Content-Type, whereas the IIS example shows CORS enabled for everything.
I have CSS that is being referenced on an external site but the font files are being blocked by the browser and wish to enable CORS on IIS7 but only for .woff, .woff2, .tff, .eot, and .svg files. I do not wish to enable CORS for everything if possible.
Right click the site you want to enable CORS for and go to Properties. Change to the HTTP Headers tab. In the Custom HTTP headers section, click Add. Enter Access-Control-Allow-Origin as the header name.
You need just need your site to send the HTTP header Access-Control-Allow-Origin with the value * to "turn off" CORs (well allow any origin).
Enable CORS Using IIS Manager Navigate to the website you need to edit the response headers for. A dialog box will open. For name enter "Access-Control-Allow-Origin" and for Value enter an asterisk ( * ). Click Ok, you are done.
Hackerman's answer is great and it led me to a solution, however, there are a few adjustments that I had to make. The first was placing the rule in the outboundRules
section under the rewrite
node.
<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>
Lastly, the regex was updated to prevent requests such as the one below, which would allow someone to request any URL across origins:
/some/critical/javascript/file.js?v=.woff
/api/secure/users?v=.woff
... but it still allows the following
/some/font.woff
/some/font.woff?etag
/some/font.woff?v=123
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With