代码改变世界

IIS7.5如何限制某UserAgent 禁止访问

2016-05-05 23:36  游乐场123  阅读(1546)  评论(0编辑  收藏  举报

参见
Blocking Bots Based on User-Agent
http://moz.com/ugc/blocking-bots-based-on-useragent

http://serverfault.com/questions/312262/how-to-block-null-blank-user-agents-in-iis-7-5

If request filtering can't handle this, you can try 'URL Rewrite' a free Add-On from Microsoft and pretty helpful anyways.

Create a rule like this:

<rule name="NoUserAgent" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_USER_AGENT}" pattern="^$" />
    </conditions>
    <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You did not present a User-Agent header which is required for this site" />
</rule>

During a quick test this worked for both an empty User-Agent and a missing one.

I'm using the regular expression '^$' which is only valid for an empty string.

You can also return a 404 or whatever else you want rather than a 403.