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.

pimvdb
  • 147
  • 1
  • 2
  • 7

3 Answers3

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.

Shane Madden
  • 112,982
  • 12
  • 174
  • 248
  • 1
    I tested it. It works. mod_php seems to override what mod_mime is doing but not mod_rewrite. – Mark Wagner Mar 08 '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 08 '11 at 20:17
  • This works great - I don't care about hackiness, as it works I'm very content. – pimvdb Mar 08 '11 at 20:55
  • @shane-madden Thanks for the correct explanation. – Mark Wagner Mar 08 '11 at 21:37
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.

TRiG
  • 1,167
  • 2
  • 13
  • 30
1

Here's another way; add to Apache config:

<FilesMatch "\.js$">
    php_value default_mimetype "text/javascript"
</FilesMatch>
qu1j0t3
  • 111
  • 2