Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
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
@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

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

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

share|improve this answer

Here's another way; add to Apache config:

<FilesMatch "\.js$">
    php_value default_mimetype "text/javascript"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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