Asp.net

IIS 的 URL 重寫規則以替換每個頁面中的文件夾路徑

  • February 17, 2016

我的網站項目中有 300 多個頁面。隨著時間的推移,我們創建了一個安全的新伺服器。該伺服器專門用於網站中的所有圖像。

所以這裡是場景:

  • 圖像的目前實現(在 aspx 中,在 css 中)
http://www.mysite.com/assets/common/image1.jpg
  • 有時在網頁和 css 中是這樣指定的
~/assets/common/image1.jpg        
  • 想用這樣的東西。
http://www.static.mysite.com/common/image1.jpg
  • 對於安全頁面
https://www.static.mysite.com/common/image1.jpg

因此,您可以看到所有圖像都來自~/assets文件夾但現在我想創建一個規則替換~/assetshttp://static.mysite.com

如何使用重寫規則在 IIS 中實現這一點。

例子:

ASPX

<img src="/assets/common/image1.jpg" id="ImageId1" alt="Image" width="100" height="100" />

<img src="http://mysite.com/assets/common/image2.jpg" id="ImageId2" alt="Image" width="100" height="100" />

想要有 IIS 規則,當找到上述程式碼時,將其替換為http://static.mysite.com/common/image1.jpg

<img src="http://static.mysite.com/common/image1.jpg" id="ImageId1" alt="Image" width="100" height="100" />


<img src="http://static.mysite.com/common/image2.jpg" id="ImageId2" alt="Image" width="100" height="100" />

您需要在 IIS 中創建出站規則。規則將需要以下內容:

  1. 前提條件應該只檢查 html 文件(我使用預設的 IsHTML)
  2. 在“匹配內容”中選擇您要檢查連結的元素
  3. 模式是^(.*)/assets/(.*)
  4. 操作屬性是http://static.mysite.com/ {R:2}。R:2 指的是上面正則表達式中的第二個()。點擊“測試模式”按鈕後,您可以檢查您需要的內容。

波紋管滿足上述的簡單規則:

在此處輸入圖像描述

你可以試試這個

<rule name="assets redirection" stopProcessing="false">
   <match url="^(.*)/(assets)/(.*)" ignoreCase="false" />
   <action type="Redirect" url="{R:1}/{R:3}" />
</rule>

它將重定向whatever/assets/common/image1.jpgwhatever/common/image1.jpg

更新:

<rule name="assets redirection" stopProcessing="false">
   <match url="^(.*)/(assets)/(.*)" ignoreCase="false" />
   <action type="Redirect" url="static.mysite.com/{R:3}" />
</rule>

引用自:https://stackoverflow.com/questions/17237170