Php – Set MIME type of PHP generated JavaScript file

.htaccessapache-2.2javascriptmimePHP

I have a dynamically created .js file by PHP. For this to work I added the following to an Apache .htaccess file:

AddHandler application/x-httpd-php .js
AddType application/javascript .js

But the .js files are sent with the MIME type text/html.

How can I make Apache still send it with MIME type application/javascript? As I have a lot of .js files, adding the header by PHP is not an option.

Best Answer

I haven't tested this, so take it with a grain of salt, but this should do it;

RewriteEngine on
RewriteRule ^.*\.js$ - [env=headerjs:1]
Header set Content-type application/javascript env=headerjs

This is pretty hackish.. if possible, reconsider doing it in the PHP code.