3

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.

3

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.

  • 1
    I tested it. It works. mod_php seems to override what mod_mime is doing but not mod_rewrite. – Mark Wagner Mar 8 '11 at 20:10
  • 1
    @embobo Actually, it would - if mod_rewrite were doing the replacement. mod_rewrite processes early on in the request, before PHP sends up content. mod_headers, though, processes right before the response is sent, giving it the power to clobber any header about to be sent. Tricky part is that it's not very flexible with conditionals, so it's using mod_rewrite for that part (and just sticking it in an env var for mod_headers to trigger on later in the request). – Shane Madden Mar 8 '11 at 20:17
  • This works great - I don't care about hackiness, as it works I'm very content. – pimvdb Mar 8 '11 at 20:55
  • @shane-madden Thanks for the correct explanation. – Mark Wagner Mar 8 '11 at 21:37
1

Here's another way; add to Apache config:

<FilesMatch "\.js$">
    php_value default_mimetype "text/javascript"
</FilesMatch>
1

Fix it within the PHP file itself: header('Content-Type: application/javascript');

And the file doesn't need a .js or .js.php extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.