iis-7 – 如何使用IIS7修复CSS文件内链接的URL重写

我正在尝试为我的朋友在家里设置一个代理服务器.我目前正在关注网站上的教程( http://blogs.iis.net/carlosag/archive/2010/04/01/setting-up-a-reverse-proxy-using-iis-url-rewrite-and-arr.aspx),但我遇到了一个奇怪的问题.

我已经尝试将/ pandora重定向到www.pandora.com但CSS文件中的链接没有改变.此外,它们仍然链接到localhost / img / ..路径.应将它们重定向到localhost / pandora / img / ..路径.

来自第一个网页的sniplet

<link rel="shortcut icon" href="/pandora/favicon.ico" type="image/x-icon" />
<link rel="icon" type="image/ico" href="/pandora/favicon.ico" />

<Meta name="viewport" content="width=device-width,initial-scale=1.0">

<link rel="stylesheet" href="css/compiled.css?v=95845013">
<link id="valanceStyle" rel="stylesheet" type="text/css" href="/pandora/static/valances/pandora/default/design.css"/>

你们能帮帮我解决这个问题吗?

解决方法

可以将出站重写规则与ARR结合使用.以下规则应该这样做:
<system.webServer>
    <rewrite>
        <outboundRules>
            <rule name="Rewrite image URLs in CSS response" preCondition="IsCSS">
                <match pattern="localhost/img/" />
                <action type="Rewrite" value="localhost/pandora/img/" />
            </rule>
            <preConditions>
                <preCondition name="IsCSS">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="text/css" />
                </preCondition>
            </preConditions>
        </outboundRules>
    </rewrite>
</system.webServer>

您当然应该使用正确的域名替换localhost.如果要从其他域名重写,则匹配标记应包含要替换的域名,操作标记应包含要替换的域名.

由于CSS不是HTML,因此您无法使用URL重写模块的标记过滤功能.因此,它只能对CSS文件的整个内容进行正则表达式匹配,这可能对大型CSS文件造成cpu密集.如果您知道需要替换多少个网址,则可以将eventss =“x”属性添加到< match>标记以限制URL重写模块必须查找的匹配数.还可以尝试将CS​​S规则移动到CSS文件的顶部.例如.:

<action type="Rewrite" value="localhost/pandora/img/" occurrences="3" />

您还可以在IIS中启用用户模式缓存,并将属性rewriteBeforeCache =“yes”添加到< outboundRules>标记让IIS缓存重写的内容.例如.:

<outboundRules rewriteBeforeCache="yes">

有关出站重写规则的更多有用信息和提示可在this blog post中找到.

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效