我很可能错过了什么。我在这篇很棒的帖子上找到了这个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中的单词进行任何限制。显然,我需要在Regex更加努力。但我们非常感谢您的帮助。
我只需要捕获与某些文件夹的相对链接,如/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>
前提条件和标记始终相同。
更新了Regex:
(^\/media\/.*)|(^\/css\/.*)