6

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.

Val
  • 17,336
  • 23
  • 95
  • 144

6 Answers6

10

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>
Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
  • If you're getting a 500 Internal Server Error with the Header line, make sure you have mod_headers enabled in your apache config. Thanks Lee! – Joel Mellon Jul 03 '13 at 18:27
9

Try:

<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
Alex
  • 447
  • 3
  • 8
  • You have to do this to get PHP To process your .js files, however, you don't need to use AddHandler inside a directive, but you would if you were using SetHandler. But when you use this handler, the MIME type of the response will default to text/html, and I don't think this can be overridden in .htaccess, which means you'll need to (re)set the MIME type in PHP, as per @Steve's answer. – Lee Kowalkowski Oct 24 '12 at 21:14
2

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");

Steve
  • 8,609
  • 6
  • 40
  • 54
1

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.

`

Marcus Recck
  • 5,075
  • 2
  • 16
  • 26
  • I know thats best, however, on my notpad++ i'd like to have the highlighters on and not black and white :) – Val May 23 '12 at 14:54
  • Can you still include using ``? Or would you have to use php to include? – JVE999 Apr 07 '14 at 20:30
0

One possibility is that you're not configured to allow the use of an .htaccess file.

You can check this in your httpd.conf.

You want to ensure that AllowOverride is set to All

eg

from

<Directory />
  Options FollowSymLinks
  AllowOverride None
  Order deny,allow
  Deny from all
  Satisfy all
</Directory>

to

<Directory />
  Options FollowSymLinks
  AllowOverride All
  Order deny,allow
  Deny from all
  Satisfy all
</Directory>
Chris Mohr
  • 3,639
  • 1
  • 13
  • 9
  • .htaccess works as I have tested with `RewriteRule . /index.php [L]` and some other bits there (which I tried removing them all, but the `AddHandler application/x-httpd-php .js` – Val May 23 '12 at 14:56
0

I don't know much about .htaccess but I do believe part the solution to your problem is to move the jQuery outside of the php tags as so:

<?php

?>
</script>
    $(document).ready();
<script>

your file is going to be parsed as any other php file, so your javascript will execute as normal, inside of javascript tags.

if you would like to include php variables inside your javascript just do as so:

<?php
    $time=time();
?>

    <script>
        $( document ).ready(function(){
            $("SPAN").text("<? echo $time; ?>");
        });
    </script>
    <SPAN></SPAN>

the above code will return the current epoch timestamp inside of the SPAN element