简体   繁体   English

在web.config中将子文件夹重写为子域

[英]Rewrite Subfolder to Subdomain in web.config

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... 大多数图片都是从Joomla提供的,所以虽然域的根类似于domain.com,但大多数图像都是用src =“/ images / folder / picture.jpg”输入的。不太确定这是如何影响重写的,但是关于cheesemacfly在下面的答案中没有一个选项正在运作......

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. 更新2:虽然在我的特殊情况下,cheesemacfly无法帮助我,但我给了他赏金,并将他的答案标记为被接受的答案,因为他超越了我试图帮助我聊天。 Hopefully his answer will help someone with rewrites on IIS. 希望他的回答可以帮助重写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. 为了能够将URL重写(并且不仅仅是重定向)到外部网站,您需要安装应用程序请求路由模块并启用代理模式。

To do so: 为此:

  1. Download and install the module 下载并安装该模块
  2. Open your IIS management console ( inetmgr ) 打开IIS管理控制台( inetmgr
  3. Select Your server node 选择您的服务器节点
  4. Double click on Application Request Routing Cache : 双击Application Request Routing Cache ARR
  5. Click on Server Proxy Settings on the Actions pane (right of the screen) 单击Server Proxy Settings Actions窗格(屏幕右侧)上的“ Server Proxy Settings
  6. Check the box Enable proxy and click on Apply 选中Enable proxy框,然后单击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? ") 请参阅: http//www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (“ 您应该使用哪个选项? ”部分)

TIP: 小费:

The best way to test your pattern is to use the IIS test pattern tool. 测试模式的最佳方法是使用IIS测试模式工具。

At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern: 在您网站的根目录 - > URL重写 - >创建空白规则 - >单击测试模式: 模式测试

If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool 如果未获得预期结果,则可以使用“ 失败请求跟踪”工具调试重写

NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. 注意:将规则更改为重定向而不是重写可以解决问题。 Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet. 理想情况下,你希望它是一个重定向,但我花了很多时间试图让重写工作,到目前为止还没有解决方案。

<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM