3

I have an empty file and for some reason Apache refuses to return the content length with the value set to 0. When making a basic GET request, the Content Length is returned as 0, but for HEAD requests there is no Content Length header. How can I force Apache to return a content length of 0 for empty files?

I have attempted to use PHP and htaccess to achieve this, but PHP functions such as header does not work.

Rob Gates
  • 275
  • 5
  • 14
  • @apokryfos I disagree. According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4, "The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request". There is no content length header present in the HEAD request, but one present in the GET request. This inconsistency does not make sense. – Rob Gates Oct 01 '19 at 15:41
  • The RFC you're quoting is obsolete. [The newer one](https://tools.ietf.org/html/rfc7230#section-3.3.2) says: *A server MAY send a Content-Length header field in a response to a HEAD request (Section 4.3.2 of [RFC7231]); a server MUST NOT send Content-Length in such a response unless its field-value equals the decimal number of octets that would have been sent in the payload body of a response if the same request had used the GET method.* Meaning that it is optional to send it in a HEAD request. I do agree that there should be a way to make Apache provide it – apokryfos Oct 01 '19 at 15:43
  • Is this PHP related though? If the content is coming from the PHP handler then apache is not what's coming into play here – apokryfos Oct 01 '19 at 15:45
  • @apokryfos I mentioned PHP to express that I am open to answers that use a PHP trick. – Rob Gates Oct 01 '19 at 15:48
  • This may be helpful https://stackoverflow.com/questions/21574062/apache-2-2-2-response-on-head-requests (if not there's a few more links there) – apokryfos Oct 01 '19 at 15:51

0 Answers0