Rewrite Subfolder to Subdomain in web.config(在 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:
- Download and install the module
- Open your IIS management console (
inetmgr
) - Select Your server node
- Double click on
Application Request Routing Cache
: - Click on
Server Proxy Settings
on theActions
pane (right of the screen) - Check the box
Enable proxy
and click onApply
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 中将子文件夹重写为子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 web.config 中将子文件夹重写为子域
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01