![](/img/trans.png)
[英]How to create web.config URL Rewrite to redirect a subdomain to a pageroute
[英]Rewrite Subfolder to Subdomain in web.config
我正在嘗試為以下場景編寫重寫規則。
用戶嘗試加載此圖片:
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重寫(並且不僅僅是重定向)到外部網站,您需要安裝應用程序請求路由模塊並啟用代理模式。
為此:
inetmgr
) Application Request Routing Cache
: Server Proxy Settings
Actions
窗格(屏幕右側)上的“ Server Proxy Settings
Enable proxy
框,然后單擊Apply
第二步是建立規則。
如果您希望重寫基於路徑,請使用以下代碼:
<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重寫 - >創建空白規則 - >單擊測試模式:
如果未獲得預期結果,則可以使用“ 失敗請求跟蹤”工具調試重寫
注意:將規則更改為重定向而不是重寫可以解決問題。 理想情況下,你希望它是一個重定向,但我花了很多時間試圖讓重寫工作,到目前為止還沒有解決方案。
<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.