Php – mod_headers not sending headers when file is PHP

apache-2.2http-headersPHP

I've got my .htaccess file set to the following:

 Header set MyHeader "I'm Set!"

If I go to that directory (http://example.com/test/), which has no default index file, and view the network activity (in this case with Chrome), I can see that the header is being sent. If I go to a php file (http://example.com/test/test.php), the header isn't sent.

I've created an index.html (with simply <pre>Hi as the content) and it gets sent. I change the file name to .php, no header. If I change the orignal test.php to test.html, it outputs the garbage-looking code, and the header is sent. If I change test.php to test.png, I get a broken image icon, and the header is sent.

So, without testing beyond the above in terms of mime-type and file extensions, it appears only .php files (legit or otherwise) are either set to not have .htaccess rules apply or specifically the mod_headers directives. But I don't know how to test for this and because it's a shared server, I don't have httpd.conf access to poke around.

Any ideas as to why apache would skip sending headers for php files?

I've also tried:

 <FilesMatch "\.(php)$">
     Header set MyHeader "I'm Set!"
 </FilesMatch>

Best Answer

This is expected behavior when running CGI scripts. From the latest (2.4) version of the mod_headers docs:

The default value of onsuccess may need to be changed to always under the circumstances similar to those listed below. Note also that repeating this directive with both conditions makes sense in some scenarios because always is not a superset of onsuccess with respect to existing headers:

[...]
You're modifying or removing a header generated by a CGI script, in which case the CGI scripts are in the table corresponding to always and not in the default table.

The original issue (with, perhaps, a better explanation) is in Bug 49308.