File extension handling

Most web servers have a default configuration mapping file extensions like .html or .txt to corresponding mime-types like text/html or text/plain. The focus in this section is on handling multiple extensions like .html.txt - in general the last extension part (.txt in .html.txt) defines the mime-type:

  • file.html shall use mime-type text/html
  • file.html.txt shall use mime-type text/plain
  • file.html.wrong shall use mime-type text/plain (but especially not text/html)

Apache's mod_mime documentation explains their handling of files having multiple extensions. Directive TypesConfig and using a mime.types map probably leads to unexpected handling of extension .html.wrong as mime-type text/html:

AddType text/html     html htm
AddType image/svg+xml svg svgz
Copied!

Global settings like shown in the example above are matching .html and .html.wrong file extension and have to be limited with <FilesMatch>:

<FilesMatch ".+\.html?$">
    AddType text/html     .html .htm
</FilesMatch>
<FilesMatch ".+\.svgz?$">
    AddType image/svg+xml .svg .svgz
</FilesMatch>
Copied!

In case these settings cannot be applied to the global server configuration, but only to .htaccess it is recommended to remove the default behavior:

.htaccess
RemoveType .html .htm
RemoveType .svg .svgz
Copied!

The scenario is similar when it comes to evaluate PHP files - it is totally expected and fine for files like test.php (ending with .php) - but it is definitively unexpected for files like test.php.html (having .php somewhere in between).

The expected default configuration should look like the following (adjusted to the actual PHP script dispatching via CGI, FPM or any other type):

.htaccess
<FilesMatch ".+\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>
Copied!