修改方法
服务器环境是IIS7并已经安装好URLREWRITE插件。
用文本编辑器打开WEB.CONFIG,此文件一般放在IIS7网站的根目录中。
找到
<system.webServer> <rewrite> <rules> </rules> </rewrite> </system.webServer>
这些节,如果没有,你可自己写上去。
注意了,<rules> </rules>这对 之间放伪静态规则还有其他规则
在<rules> </rules>之间 加入以下规则
<rule name="BlockDirectDownload" enabled="true" stopProcessing="true"> <match url="(?:doc|ppt|xls|docx|pptx|xlsx|rtf|txt|swf|pdf|wps|dps|et|wpt|dot|pps|pptm|potx|pot|ett|xlt|csv)$" ignoreCase="true" /> <conditions> <add input="{HTTP_REFERER}" pattern="^http://(.*\.)?(abc\.com)/.*$" negate="true" /> </conditions> <action type="CustomResponse" statusCode="404" /> </rule>
完成之后是以下的样子
<system.webServer> <rewrite> <rules> <rule name="BlockDirectDownload" enabled="true" stopProcessing="true"> <match url="(?:doc|ppt|xls|docx|pptx|xlsx|rtf|txt|swf|pdf|wps|dps|et|wpt|dot|pps|pptm|potx|pot|ett|xlt|csv)$" ignoreCase="true" /> <conditions> <add input="{HTTP_REFERER}" pattern="^http://(.*\.)?(abc\.net)/.*$" negate="true" /> </conditions> <action type="CustomResponse" statusCode="404" /> </rule> </rules> </rewrite> </system.webServer>
这里解释一下<rule>与</rule>之间的一些指令
name是规则的名字,一定不能与其他规则重名
match url=这里写上哪些类型的文件需要防盗,$表示在URL的结尾,(|)这里表示里面的所有类型都要防
<conditions>表示以上规则要满足这里的条件才工作,http_reffer是指来路域名,pattern是指前者值,也就是说只要是从abc.net来的都可以下载前面提到的文档,其他域名过来的都不行。
action是指生效后要做什么,这里是说要把非法访问都转向到404页面。