我很可能错过了什么。我在这篇文章中找到了这个 URL 重写示例:http://marisks.net/2017/05/14/changing-static-resource-urls-to-cdn-urls-with-url-rewrite/
我的设置是相同的。我的代码中有相对 URL,我想即时重写它们以指向我的 CDN。
<outboundRules>
<clear />
<rule name="CDN" preCondition="CheckHTML" enabled="false" stopProcessing="true">
<match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(?!www.|(?:http|ftp)s?://|[A-Za-z]:\|//).*.(jpg|jpeg|png|js|css|mp4).*)" />
<action type="Rewrite" value="http://cdn.example.com{R:1}" />
<conditions logicalGrouping="MatchAll">
</conditions>
</rule>
<preConditions>
<preCondition name="CheckHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
<customTags>
<tags name="Video">
<tag name="source" attribute="src" />
</tags>
</customTags>
</outboundRules>
这很好用。除了我需要微调对特定文件夹的重写。我知道这听起来很简单,但是当我查看上述脚本中的正则表达式时 - 我看不出我可以在哪里对我的相对 URL 中的单词进行任何限制。显然,我需要在正则表达式中更加努力地工作。但任何帮助都是值得赞赏的。
我只需要捕获某些文件夹的相对链接,如/media 或/css("/media/media.mp4"或"/css/base.css"(。
我解决了这个问题。至少,它适用于我设置为重写的文件夹,并忽略我未指定的文件夹。我会做更多的测试,但这现在有效。
<rule name="CDN" preCondition="CheckHTML" enabled="true" stopProcessing="true">
<match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(/(media|css)+)*/.*)" />
<action type="Rewrite" value="http://cdn.example.com{R:0}" />
<conditions logicalGrouping="MatchAll">
</conditions>
</rule>
前提条件和标签在整个过程中是相同的。
更新了正则表达式:
(^/media/.*)|(^/css/.*)