Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <rewrite>
- <rules>
- <rule name="Ban user-agent RogueBot" stopProcessing="true">
- <match url=".*" />
- <conditions>
- <add input="{HTTP_USER_AGENT}" pattern="RogueBotName" />
- <add input="{MyPrivatePages:{REQUEST_URI}}" pattern="(.+)" />
- </conditions>
- <action type="AbortRequest" />
- </rule>
- </rules>
- <rewriteMaps>
- <rewriteMap name="MyPrivatePages">
- <add key="/PrivatePage1.aspx" value="block" />
- <add key="/PrivatePage2.aspx" value="block" />
- <add key="/PrivatePage3.aspx" value="block" />
- </rewriteMap>
- </rewriteMaps>
- </rewrite>
- public class UserAgentBasedRedirecter : IHttpModule
- {
- private static readonly Regex _bannedUserAgentsRegex = null;
- private static readonly string _bannedAgentsRedirectUrl = null;
- static UserAgentBasedRedirecter()
- {
- _bannedAgentsRedirectUrl = ConfigurationManager.AppSettings["UserAgentBasedRedirecter.RedirectUrl"];
- if (String.IsNullOrEmpty(_bannedAgentsRedirectUrl))
- _bannedAgentsRedirectUrl = "~/Does/Not/Exist.html";
- string regex = ConfigurationManager.AppSettings["UserAgentBasedRedirecter.UserAgentsRegex"];
- if (!String.IsNullOrEmpty(regex))
- _bannedUserAgentsRegex = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
- }
- #region Implementation of IHttpModule
- public void Init(HttpApplication context)
- {
- context.PreRequestHandlerExecute += RedirectMatchedUserAgents;
- }
- private static void RedirectMatchedUserAgents(object sender, System.EventArgs e)
- {
- HttpApplication app = sender as HttpApplication;
- if (_bannedUserAgentsRegex != null &&
- app != null && app.Request != null && !String.IsNullOrEmpty(app.Request.UserAgent))
- {
- if (_bannedUserAgentsRegex.Match(app.Request.UserAgent).Success)
- {
- app.Response.Redirect(_bannedAgentsRedirectUrl);
- }
- }
- }
- public void Dispose()
- { }
- #endregion
- }
- <configuration>
- <appSettings>
- <add key="UserAgentBasedRedirecter.UserAgentsRegex" value="^msnbot/1.1" />
- </appSettings>
- ...
- <system.web>
- <httpModules>
- <add name="UserAgentBasedRedirecter" type="Andies.Web.Traffic.UserAgentBasedRedirecter, Andies.Web" />
- </httpModules>
- </system.web>
- </configuration>
- SetEnvIf User-Agent ^$ spammer=yes # block blank user agents
- Order allow,deny
- allow from all
- deny from env=spammer
Add Comment
Please, Sign In to add comment