Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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
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
add comment

Here's another way; add to Apache config:

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

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
add comment

Your Answer

 
discard

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.