Apache2 mod autoindex does not read .php

616 Views Asked by At

Apache2's mod autoindex allows to include a header and a readme files, that I configured this way in the directory's .htaccess:

ReadMeName footer.html
HeaderName header.php

For some obscure reason, the header file is not read if it is a .php. I renamed it to header.html and in the htaccess too:

HeaderName header.html

and it worked perfectly (even when in .html it included <?php ?> markups)

Why does apache do hat, and is there a way to fix it?

1

There are 1 best solutions below

6
Markus AO On

Per the manual on mod_autoindex / HeaderName directive:

Filename must resolve to a document with a major content type of text/* (e.g., text/html, text/plain, etc.). This means that filename may refer to a CGI script if the script's actual file type (as opposed to its output) is marked as text/html...

...while a .php file's content-type is defined as application/x-httpd-php, even if the script's output is by default text/html. However, there's hope yet. You can actually get the PHP parsed by adding the following combo into your .htaccess before your HeaderName directive:

AddHandler application/x-httpd-php .php
AddType text/html .php

Neither of the two on their own will do the job. I presume that here Apache (tested on 2.4) first sets a handler and parses the .php file, and then agrees to understand that the output type is majorly text/html indeed. I'm calling this quirky, but it's working!