Parse js/css as a PHP file using htaccess

前端 未结 6 1841
面向向阳花
面向向阳花 2020-12-10 07:36

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-

相关标签:
6条回答
  • 2020-12-10 07:48

    Try:

    <FilesMatch "\.(js)$">
    AddHandler application/x-httpd-php .js
    </FilesMatch>
    
    0 讨论(0)
  • 2020-12-10 07:50

    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>
    
    0 讨论(0)
  • 2020-12-10 07:50

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

    0 讨论(0)
  • 2020-12-10 07:50

    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

    0 讨论(0)
  • 2020-12-10 07:51

    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.

    `

    0 讨论(0)
  • 2020-12-10 08:06

    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>
    
    0 讨论(0)
提交回复
热议问题