【问题标题】:Stopping hotlinking from a directory using web.config使用 web.config 从目录停止热链接
【发布时间】:2012-04-19 08:59:10
【问题描述】:

我的网站上有一个用于缓存大型 Flash 电影的文件夹,我想阻止其他人将它们嵌入他们的网站;我想尝试仅使用web.config 文件来执行此操作。这怎么可能?

我对规则的第一次尝试(不起作用):

以下规则应该阻止公共访问(和嵌入)缓存文件夹“CurrentCache”-http://myurl.com/ContentCache/ 中的 .swf 文件,并改为提供替换电影“NoEmbedFromCacheSWF.swf”。

<rule name="Prevent SWF hotlinking" enabled="true">
      <match url="^(ContentCache)(.swf)$" ignoreCase="true" />
      <conditions>
        <add input="{HTTP_REFERER}" pattern="^http://(.*\.)?myurl\.com/.*$" negate="true" />
      </conditions>
      <action type="Rewrite" url="/Content/Flash/NoEmbedFromCacheSWF.swf" />
    </rule>

提前致谢!

注意:我认为我在&lt;match url="A swf inside /ContentCache/" ignoreCase="true" /&gt; 行中弄错了正则表达式,有什么想法吗?

【问题讨论】:

    标签: asp.net .net web-config url-rewrite-module


    【解决方案1】:

    您可以为此构建一个HttpModule。有一篇博客文章准确地描述了我认为你想要做什么:

    HttpModule to block external referrers in ASP.NET

    编辑:当然,我只是在此处违反关于web.config 的规则。您必须使用外部模块,但您可以使用它仅从 web.config 引用而无需修改任何代码。

    Edit2:如果你想使用重写规则,你必须改变你的模式,像这样:

    <rule name="Prevent SWF hotlinking" enabled="true">   
      <match url="/ContentCache/.*\.swf$" ignoreCase="true" />   
      <conditions>   
        <add input="{HTTP_REFERER}" pattern="^http://(.*\.)?myurl\.com/.*$" negate="true" />   
      </conditions>   
      <action type="Rewrite" url="/Content/Flash/NoEmbedFromCacheSWF.swf" />   
    </rule>  
    

    使用的模式是一个正则表达式,你可以阅读它们here,你可以测试它们,例如this webpage

    【讨论】:

    • 是的,我可以看到这行得通,但我真的在寻找一个简单的无代码解决方案,像 it-notebook.org/iis/article/prevent_hotlinking_url_rewrite.htm - 这是一个用于图像的解决方案 - 但我遇到的真正问题是不知道该写什么对于&lt;match url="???" /&gt; 部分。
    • 最终的编辑看起来很完美,我试过了,但不幸的是它仍然让其他具有不同 url 的网站在我的 contentcache 文件夹中获取 swfs。我认为你非常接近!
    • 你检查过网页发送的referer值吗?您还可以尝试删除条件以查看重写是否至少(对于所有页面)有效,然后尝试读取条件。对我来说看起来不错...
    • 你检查重定向后在浏览器中获得的 URL 了吗?
    • 好吧,因为它是重写的,所以浏览器中的 url 没有改变......但不用担心......感谢@aKzenT,我现在可以正常工作了!我认为我的行动路线中有一个错字,因为我从网上重新复制并编辑了一个,现在它很有魅力:towerdefence.me/ContentCache/KingdomRush_1_073.swf再次感谢!
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    相关资源
    最近更新 更多