Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse js/css as a PHP file using htaccess

Hi I have been trying so hard to do the following on htaccess but it does not seem to work.

can someone out there help me?

AddType application/x-httpd-php .js

I tried the php4,php5 version all the things I could find on google but I had no luck.

what have I missed?

in my file.js

are the following...

<?php 

$(document).ready();//jquery/javascript
?>

I am expect a php fatal error but I see exactly the above as plain text.

like image 673
Val Avatar asked May 23 '12 14:05

Val


4 Answers

If you want to do this using just .htaccess configuration (who wants to set the headers in every PHP file?):

<FilesMatch "\.css$">
  SetHandler application/x-httpd-php
  Header set Content-type "text/css"
</FilesMatch>

<FilesMatch "\.js$">
  SetHandler application/x-httpd-php
  Header set Content-type "application/javascript"
</FilesMatch>
like image 153
Lee Kowalkowski Avatar answered Nov 04 '22 04:11

Lee Kowalkowski


Try:

<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
like image 27
Alex Avatar answered Nov 04 '22 06:11

Alex


If I am correct, your PHP JS file has to be served with a header of content-type: application/x-javascript, otherwise it is not interpreted as JS.

Header("content-type: application/x-javascript");

like image 43
Steve Avatar answered Nov 04 '22 06:11

Steve


If you're trying to make PHP work in Javascript files you can simply rename the Javascript to javascript_file.js.php and have PHP work inside of that.

You will have no problem including that into your page.

`

like image 1
Marcus Recck Avatar answered Nov 04 '22 04:11

Marcus Recck