IIS 7 URL重写规则适用于网站本身未引用的图像

新的URL重写与IIS 7我需要redirect到另一个页面的网站本身没有引用的图像。 例如,某人在浏览器中input“www.somesite.com/images/image001.jpg”,会将用户redirect到“www.somesite.com/prevent.aspx?image001.jpg”。

我发现了一些很好的技巧来防止盗链: http : //blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx ,这很好,但没有像我想要从上面的例子。

重写防止盗链的规则:

<rule name="Prevent image hotlinking"> <match url=".*\.(gif|jpg|png)$"/> <conditions> <add input="{HTTP_REFERER}" pattern="^$" negate="true" /> <add input="{HTTP_REFERER}" pattern="^http://ruslany\.net/.*$" negate="true" /> </conditions> <action type="Rewrite" url="/images/say_no_to_hotlinking.jpg" /> </rule> 

重写redirect的规则:

 <rule name="Query String Rewrite"> <match url="page\.asp$" /> <conditions> <add input="{QUERY_STRING}" pattern="p1=(\d+)" /> <add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*p2=(\d+)" /> </conditions> <action type="rewrite" url="newpage.aspx?param1={C:1}&amp;amp;param2={C:2}" appendQueryString="false"/> </rule> 

无论如何以某种方式结合规则做我想要的?

提前致谢

如果你想停止热连接,并想通知用户它通常要发送一个虚拟的图像回文本说,不允许热链接。 将请求redirect到一个页面,因为这对用户来说很可能是不可见的。

图片加载了<img>标签,尽pipe浏览器将遵循redirect,但总是希望得到某种支持的图片。 如果请求被redirect到HTML页面,则浏览器将不会显示图像或显示收到无效图像的图标。 图像热链接的页面仍然显示。

所以Ruslan Yakushev提出的重写规则实际上是正确的处理方式。 您也可以使用例如以下操作进行redirect:

 <action type="Redirect" url="http://www.somesite.com/prevent.aspx?{REQUEST_FILENAME}" appendQueryString="false" redirectType="Found" /> 

但最终结果将是一样的。 不过,您将无法将主浏览器窗口redirect到其他页面。