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

查看:194
本文介绍了重写子文件夹在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>

更新:添加额外的信息。大多数的图片正在从的Joomla担任了如此而域的根是像domain.com,大多数图像是具有SRC =/图片/文件夹/ p​​icture.jpg不太清楚这是怎么影响改写输入,但没有选项下面cheesemacfly的回答,正在...
UPDATE2:虽然cheesemacfly无法帮助我在我的特殊情况下,我为他颁发了奖金,标志着他的回答是公认的一个,因为他去超越,试图帮助我在聊天。但愿他的回答会帮助别人在IIS上重写。

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.

推荐答案

编辑:

要能够重写(而不是只重定向)的URL外部网站,您需要安装的应用程序请求路由模块并启用了代理模式。

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.

要做到这一点:


  1. 下载并安装该模块

  2. 打开您的IIS管理控制台( INETMGR
  3. 选择您的服务器节点

  4. 应用程序请求路由缓存双击

  5. 单击关于服务器代理设置操作窗格(屏幕的右侧)

  6. 勾选启用代理服务器,然后点击应用

  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.

如果你希望你的改写是基于路径上,然后使用下面的code:

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>

请参考:的http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (参见哪一个选项应该使用?

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

提示:

要测试你的模式,最好的办法是使用IIS测试模式的工具。

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

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

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

如果你没有得到预期的结果,您可以使用调试重写的<一个href=\"http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules\"相对=nofollow>失败请求跟踪工具

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

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

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