在CSS文件中重写URL

在CSS文件中重写URL,css,url-rewriting,iis-8,url-rewrite-module,web-config,Css,Url Rewriting,Iis 8,Url Rewrite Module,Web Config,我正在尝试设置URL重写,以重写对图像等的所有引用,以指向我的CDN 我让它使用以下web.config设置处理.NET页面中的任何引用: <outboundRules> <rule name="Outbound CDN: fonts" preCondition="IsTextContent"> <match filterByTags="None" pattern="^/fonts/(.+)$" /> <action ty

我正在尝试设置URL重写,以重写对图像等的所有引用,以指向我的CDN

我让它使用以下web.config设置处理.NET页面中的任何引用:

<outboundRules>
    <rule name="Outbound CDN: fonts" preCondition="IsTextContent">
    <match filterByTags="None" pattern="^/fonts/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: images" preCondition="IsTextContent">
        <match filterByTags="A, Img, Link" pattern="^/images/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: pdf" preCondition="IsTextContent">
        <match filterByTags="A, IFrame" pattern="^/pdf/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: scripts" preCondition="IsTextContent">
        <match filterByTags="Script" pattern="^/scripts/(.+)$" />
        <action type="Rewrite" value="http://cdn.mydomain.com{R:0}" />
    </rule>
    <rule name="Outbound CDN: style" preCondition="IsTextContent">
        <match filterByTags="Link" pattern="^/style/.+\.(?:css|less|sass|master|cs)$" />
        <action type="Rewrite" value="http://mydomain.com{R:0}" />
    </rule>
    <preConditions>
        <preCondition name="IsTextContent">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)$" />
        </preCondition>
    </preConditions>
</outboundRules>

但是,我不知道如何重写CSS文件中引用的图像和字体的引用

有没有人对如何对CSS中引用的图像和字体进行类似的URL重写有什么想法


我使用的是Windows Server 2012R2、IIS 8.5、.NET 4.5.1以及最新版本的URL重写和应用程序请求路由模块。

此处使用web.config代替HTTP模块/处理程序、应用程序路由或IIS驱动的重定向有何特殊原因?我使用的是URL重写模块,这是一个可配置的IIS级模块,专为此目的而设计。IIS重定向将导致301响应,这是一个性能问题。这意味着每个请求都有两个响应。。。对原始URL的第一个请求的301响应和对CDN URL的请求的200响应。这是一个很大的HTTP流量开销,这将否定利用CDN的任何优势。啊,我的意思是“重写”而不是“重定向”。Url重写模块的文档记录很差(相对而言),缺乏mod_Rewrite的功能,但具有不调用托管运行时堆栈的优点。无论如何,我并不是建议您将301/302重定向到您的CDN。我也不认为重写CDN是一个好主意,因为同样的原因重定向不是。您确实希望在源HTML级别更改这些引用,以便它们根本不会影响您的站点。请参阅对等的评论。硬编码CDN url将成为可管理性的噩梦。我们有一个临时站点和一个生产站点。我们需要用一个变量对整个站点中的每个图像、脚本、字体等进行编码,以及使用不同的CSS文件等。通过生成适当的CDN URL作为构建过程中的正式步骤,您可以在构建时解决这一噩梦。别担心,在你的情况下这是不可能的。无论如何,浏览器将对CSS中的每个链接资源发出请求。这些应该像其他任何请求一样被重写规则捕获。