10

我的网站项目中有 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" />
4

3 回答 3

11

您需要在 IIS 中创建出站规则。规则将需要以下内容:

  1. 前提条件应该只检查 html 文件(我使用默认的 IsHTML)
  2. 在“匹配内容”中选择您要检查链接的元素
  3. 模式是^(.*)/assets/(.*)
  4. 操作属性是http://static.mysite.com/ {R:2}。R:2 指的是上面正则表达式中的第二个()。单击“测试模式”按钮后,您可以检查您需要的内容。

波纹管满足上述的简单规则:

在此处输入图像描述

于 2013-06-26T09:57:25.727 回答
5

你可以试试这个

<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>
于 2013-06-26T08:51:54.117 回答
2

umm some how you can do like this

string imageUrl= Request.Url.Scheme + "://" + Request.Url.Authority + "/Image/logo/Logo.png";

Scheme is your web Schemes http or https etc. Authority is your web domain name with port if any. Then here it goes your image url completely.

I have used this logo url for the ssrs report service and got fine.Hope it ll work for you.

Comments and queries are welcome . Thank you.

于 2013-06-27T11:30:12.787 回答