We have an ASP.Net MVC3 site only accessible over HTTPS, by using the RequireHTTPS attribute on the controller.
We are receiving numerous HTTP HEAD method requests, mainly from what appear to be Twitter bots. The default ASP.Net/MVC3 response is a '500 Internal Server Error', and are being caught/logged by elmah and log4net (now filtered out!).
I could write a specific controller and route to handle these non-HTTPS requests as per this question - Responding to HEAD Request in asp.NET MVC 3.
But, from the bots perspective what would be the best response? 200 to show the server is alive, a 302 redirect to the HTTPS url, or stick with the 500 as the site isn't accessible over HTTP?
You could respond with
which means
or with
which means
Personally, I would go with the
405
since it's an error on the client side, a "Hey man, we don't serve that stuff here." seems more appropriate to me than "What the hell are you talking about? I don't understand it." one, the latter is suggested by the the server does not recognize the request method bit of the501
description.All the HTTP status codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
In my case, I was only getting HEAD requests on the root of the site
/
which seems like bots probing. So, I was a bit worried about returning a 500 or 404.More on 405
405 may be OK as per Albireo's answer, but you need to return the accepted verbs, something like:
302 option
Looking at the comment in the MVC code which does not redirect the HEAD request:
It seems like another option is to send a 302. It should be reasonably safe to return a 302 to the HTTPS site for bot HEAD requests to root (which is what MVC does for a GET). So, I implemented the following which is based on the way that MVC does it:
Implement in global.asax.cs: