I want .js files in some very specific folder to be rendered with PHP by Apache. Where can I set it, and how?

In other words: I want apache to deal with files with extension js, as if they are with extension php... but only in specific folder of /var/www

  • This is confusing, please add more info on what you want to do. PHP is server side, .js is rendered client side. The Apache server never does anything with .js other than serving as a text file (with proper headers). Jun 26, 2011 at 16:28
  • Yes, I know, but afaik you can tell apache how to behave with every file extension. Now I need to define that behavior for very specific folder.
    – David
    Jun 27, 2011 at 7:01

2 Answers 2


I think i know what you mean now. You want a js file to be sent to the client, but you want to process it as PHP so you can modify the js file before sending it to the client.

If so, try:

<Directory /var/www/somespecialjsfolder >
AddType application/x-httpd-php .js

You'll probably need to add PHP code to set mime-type to application/x-javascript on output of your js.

That said, this is probably a bad idea. You don't want to have to debug Javascript which is different for every client. Put all your client customizations in the js file, keep it consistent. Abstract things out with JQuery or equivalent.


What is the content of these .js files ? Is it PHP or JavaScript ?

Do be more precise: do you want Apache to read the contents of this files as PHP, because it is, only with an uncommon extension, or do you want Apache to process JavaScript instructions server-side ?

If it's the latter, I don't know of any simple way to do this. It would involve parsing the contents of the .js files, and rewrite this JavaScript in PHP, assuming it is code for which there is sense to execute it on the server...

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.