title: How to block a specific user agent in Apache
body: <p>I'm configuring my Django app to email me errors (exceptions).</p>
  
  <p>Normally no problem - but my email is hosted on Office 365, and it seems that Microsoft is automatically scanning and loading URLs within emails.  </p>
  
  <p>The result is that it hits the URL in my Django app, and causes another error... and another email.  End result: a charming little mail loop which sends me 50+ messages within a few seconds.</p>
  
  <p>I found entries like this in my apache logs:</p>
  
  <pre><code>157.55.39.163 - - [22/Aug/2018:17:30:05 +0000] "GET /testerror HTTP/1.1" 500 5808 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534+ (KHTML, like Gecko) BingPreview/1.0b"
  </code></pre>
  
  <p>I want to block access to the user agent (containing "BingPreview"), so I can prevent this loop.</p>
  
  <p>I put this into my virtualhost:</p>
  
  <pre><code>SetEnvIf User-Agent "^.*BingPreview.*$" bad_user
  
  &lt;Directory /path/top/my/app/&gt;
     &lt;Files wsgi.py&gt;
         Require not env bad_user
     &lt;/Files&gt;
  &lt;/Directory&gt;
  </code></pre>
  
  <p>But when I reload apache, I get the error <code>negative Require directive has no effect in &lt;RequireAny&gt; directive</code>.</p>