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

发布于 2024-12-23 01:34:09 字数 972 浏览 3 评论 0原文

我正在尝试为家里的朋友设置代理服务器。我目前正在关注网站上的教程(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/.. 路径。

第一个网页的片段

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

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

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.

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-12-30 01:34:09

可以通过出站重写规则与 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 密集型的。如果您知道需要替换多少个 URL,则可以将 occurrences="x" 属性添加到 标记中,以限制 URL 重写的匹配数量模块必须寻找。另请尝试将 CSS 规则移至 CSS 文件的顶部。例如:

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

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

<outboundRules rewriteBeforeCache="yes">

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

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>

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文