Using PHP inside .html files not working as expected

7,991

Solution 1

<!--?php echo "It works!"; ?-->

This is what you see when you analyse the element using the browsers Object Inspector ie. the interpreted source. This is not the HTML source as sent from the server (ie. "view source").

Basically, your HTML pages are not being processed by PHP. You probably need to check with your host as to the correct directive to use. You may need to use AddHandler instead, or drop the 5, or something else. (That is if your host permits these directives in .htaccess?)

Solution 2

My apache installation has a php5.conf file I added this to it.

<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>

In my .htaccess file I also changed

AddType application/x-httpd-php5 .html

to

AddType application/x-httpd-php .html

Scott

Solution 3

It's usually better to just use a php file instead of an html file for what you're trying to do. However, if you absolutely need it to be an html document, you can use an .htaccess file placed in the root of your site containing this:

AddType application/x-httpd-php .htm .html

Instead of

AddType application/x-httpd-php5 .htm .html
Share:
7,991

Related videos on Youtube

ScottD
Author by

ScottD

Updated on September 18, 2022

Comments

  • ScottD
    ScottD over 1 year

    Apache2, Linux, php5

    I have added this line to my .htaccess file

    AddType application/x-httpd-php5 .htm .html
    

    running apace2ctl -m, shows this module loaded

    A php5_module (shared)

    I have a simple HTML page with one line of PHP code inside an <h2> tag

     <?php echo "It works!"; ?>
    

    When rendered in the browser the server appears to have commented the PHP out like so

    <!--?php echo "It works!"; ?-->
    

    If I rename the file with a PHP extension it works fine.

    Why won't Apache process the PHP code in my HTML files as directed? What am I doing wrong?