asp.net - 将 web.config 中的子文件夹重写为子域

标签 asp.net iis-7 web-config subdomain url-rewrite-module

我正在尝试为以下场景编写重写规则。

用户尝试加载此图片:

domain.com/images/folder/picture.jpg

相反,我需要加载它:

cdn.domain.com/images/folder/picture.jpg.

这是我所拥有的不起作用的内容:

<rule name="CDN rewrite for Images">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="domain.com" />
        <add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
    </conditions>
    <action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>

更新:添加附加信息。大多数图片都是从 Joomla 提供的,因此虽然域的根类似于domain.com,但大多数图像都是使用 src="/images/folder/picture.jpg"输入的,不太确定这如何影响重写,但下面的Cheesemacfly答案中的所有选项都不起作用......

更新2:虽然cheesemacfly在我的特殊情况下无法帮助我,但我授予了他赏金并将他的答案标记为已接受的答案,因为他竭尽全力尝试在聊天中帮助我。希望他的回答能够对 IIS 重写的人有所帮助。

最佳答案

编辑:

为了能够重写(而不仅仅是重定向)外部网站的 URL,您需要安装 Application Request Routing module并启用代理模式。

要这样做:

  1. Download and install the module
  2. 打开 IIS 管理控制台 (inetmgr)
  3. 选择您的服务器节点
  4. 双击应用程序请求路由缓存: ARR
  5. 点击操作 Pane (屏幕右侧)上的服务器代理设置
  6. 选中启用代理复选框,然后单击应用 proxy

第二步是设置规则。

如果您希望重写基于路径,请使用以下代码:

<rewrite>
  <rules>
    <rule name="Rewrite to cdn domain">
      <match url="^images/folder/(.+)$" />
      <action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
    </rule>
   </rules>
</rewrite>

或者,如果您在第二个网站上保留相同的文件夹架构,则可以按如下方式简化:

<rewrite>
  <rules>
    <rule name="Rewrite to cdn domain">
      <match url="^images/folder/(.+)$" />
      <action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
    </rule>
   </rules>
</rewrite>

如果您只想捕获以特定扩展名结尾的文件(例如图像):

<rewrite>
  <rules>
    <rule name="Forward to cdn domain">
      <match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
      <action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
    </rule>
  </rules>
</rewrite>

请引用:http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (“您应该使用哪个选项?”部分)

提示:

测试模式的最佳方法是使用 IIS 测试模式工具。

在网站的根目录 -> URL 重写 -> 创建空白规则 -> 单击测试模式: Pattern test

如果没有得到预期的结果,您可以使用 Failed Request Tracing tool 调试重写。

关于asp.net - 将 web.config 中的子文件夹重写为子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14269149/

相关文章:

c# - 无法将 WCF 行为扩展添加到端点行为

c# - 动态编译的 ASP.NET 网站

c# - 'User.IsInRole' 中的多个角色

c# - 我的 ListView 不会输出,是否需要回发?

asp.net - 使 Web.config 转换在本地工作

asp.net - 如何在 IIS7.0 上解析 "HTTP Error 500.19 - Internal Server Error"

c# - 应用程序池身份与模拟身份?

c# - 在设计 View 中使用 ASP.NET 时,Visual Studio 2012 SP3 更改链接 href

asp.net - 如何将自定义声明添加到 ACS 服务身份

c# - 如何正确配置此 IIS 7.5 子应用程序?