如何使用IIS7修复CSS文件内链接的URL重写 [英] How to fix URL Rewriting for links inside CSS files with IIS7

查看:154
本文介绍了如何使用IIS7修复CSS文件内链接的URL重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to set up a proxy server for my friends back at home. I'm currently following the tutorial on the web site (http://blogs.iis.net/carlosag/archive/2010/04/01/setting-up-a-reverse-proxy-using-iis-url-rewrite-and-arr.aspx) but I've come across a strange problem.

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

I've tried making /pandora redirect to www.pandora.com but the links inside the CSS files are not changing. Furthermore they are still linked to the localhost/img/.. path. They should be redirected to the localhost/pandora/img/.. path.

来自第一个网页的sniplet

sniplet from the first webpage

<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"/>

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

Can you guys help me fix this problem?

推荐答案

可以将出站重写规则与ARR结合使用。以下规则应该这样做:

It is possible to do this with a outbound rewrite rule in combination with ARR. The following rule should do it:

<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。如果您要从其他域名重写,则匹配标记应包含您要替换的域名,并且操作标记应包含您要替换的域名。

You should of course replace localhost with the proper domain names. If you are rewriting from a different domain name then the match tag should contain the domain name you want to replace and the action tag should contain the domain name you want it to replace.

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

As CSS is not HTML you can not use tag filtering feature of the URL rewrite module. So it can only do regular expression matching against the whole content of the CSS file which can potentially be CPU intensive on large CSS files. If you know how many URL's need to be replaced you can add the occurrences="x" attribute to the <match> tag to limit the number of matches the URL rewrite module has to look for. Also try moving the CSS rules to the top of the CSS file. E.g.:

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

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

You can also enable user mode caching in IIS and add the attribute rewriteBeforeCache="yes" to the <outboundRules> tag to let IIS cache the rewritten content. E.g.:

<outboundRules rewriteBeforeCache="yes">

有关出站重写规则的更多有用信息和提示可在此博文

More useful info and tips about outbound rewrite rules can be found in this blog post.

这篇关于如何使用IIS7修复CSS文件内链接的URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆