在 web.config 中将子文件夹重写为子域 [英] Rewrite Subfolder to Subdomain in web.config

查看:39
本文介绍了在 web.config 中将子文件夹重写为子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

用户尝试加载此图片:

domain.com/images/folder/picture.jpg

相反,我需要加载它:

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

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

<rule name="CDN rewrite for Images"><匹配 url="(.*)"/><条件><添加输入="{HTTP_HOST}" 模式="domain.com"/><添加输入="{REQUEST_URI}" 模式="^/images/folder/(.*)$"/></条件><action type="重写" url="cdn.domain.com/images/folder/{C:1}"/></规则>

更新:添加附加信息.大多数图片都是从 Joomla 提供的,因此虽然域的根目录类似于 domain.com,但大多数图片都是使用 src="/images/folder/picture.jpg" 输入的,不太清楚这对重写有何影响,但是下面cheesemacfly的答案中没有一个选项有效...

更新 2: 虽然 cheesemacfly 在我的特殊情况下无法帮助我,但我奖励了他并将他的答案标记为已接受,因为他在聊天中竭尽全力帮助我.希望他的回答能帮助那些在 IIS 上重写的人.

解决方案

为了能够将 URL 重写(而不仅仅是重定向)到外部网站,您需要安装 (您应该使用哪个选项?")

提示:

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

在您网站的根目录 -> URL Rewrite -> 创建空白规则 -> 点击测试模式:

如果你没有得到预期的结果,你可以使用 失败的请求跟踪工具

I'm attempting to write a rewrite rule for the following scenario.

User attempts to load this picture:

domain.com/images/folder/picture.jpg

and instead, I need it to load:

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

Here's what I have that isn't working:

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

UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...

UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.

解决方案

EDIT:

To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.

To do so:

  1. Download and install the module
  2. Open your IIS management console (inetmgr)
  3. Select Your server node
  4. Double click on Application Request Routing Cache:
  5. Click on Server Proxy Settings on the Actions pane (right of the screen)
  6. Check the box Enable proxy and click on Apply

The second step is about setting up your rules.

If you want your rewrite to be based on the path then use the following code:

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

Or if you keep the same folder architecture on the second website you can simplify as follow:

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

If you want to catch only the files ending with a specific extension (let's say images):

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

Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")

TIP:

The best way to test your pattern is to use the IIS test pattern tool.

At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:

If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool

这篇关于在 web.config 中将子文件夹重写为子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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