"Configure PHP by Directory or File" documentation suggested change

I set up a local development server with multiple PHP versions following the information here https://openlitespeed.org/kb/setup-per-directory-file-php/

However when using specific PHP versions this way I had problems with pages loading - missing CSS and very slow speed. Developer tools in Firefox showed errors like this:

The stylesheet was not loaded because its MIME type, "text/html", is not "text/css"

even though the mime type was specified correctly as text/css in the HTML:

<link rel="stylesheet" type="text/css" href="assets/css/styles.css" media="all">

The problem seems to be caused by using "Force MIME Type" to set the PHP handler. All files are parsed through this and sent as text/html regardless of what they are. This results in the incorrect MIME type error and the slow performance as the server tries to parse images, CSS, Javascript and everything else as PHP.

To solve this, instead of using "Force MIME Type", I have used the "MIME Type" field and set it to:

application/x-httpd-lsphp70 php phar

This only parses php and phar files as php (as the specified version), and serves the rest following normal MIME rules. Extra file types could be added if required.

Hopefully this is useful and maybe the documentation can be amended or a note added once someone more knowledgable has done more testing.


Please try the following example,

RewriteCond %{REQUEST_URI} !\.(css|js|html|json|mustache)$
RewriteRule .* - [H=application/x-httpd-lsphp74]
Please substitute the PHP version for your need.
Thank you, and sorry for taking so long to respond.

A per-folder basis using .htaccess is much tidier and also means I can quickly change versions by editing the file and restarting the server.

A slight change though, I added .* to the end of the RewriteCond so URLs such as test.php/123 would work correctly.

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|js|html|json|mustache).*$
RewriteRule .* - [H=application/x-httpd-lsphp74]