skip to Main Content

I am writing some PHP code in my .htm and .html pages but it’s not executing the code.
I must strictly not have to change the page extension.

I have added this handler to the .htaccess page

AddHandler application/x-httpd-php74 .php .htm
AddHandler application/x-httpd-php74 .php .html

My server PHP version is PHP 7.4

What I am missing, how to use PHP code in .htm and .htm pages?

I am doing this on siteguard hosting so if any server configuration I may missing?

4

Answers


  1. Add the following to your .htaccess

    AddType application/x-httpd-php .html
    
    Login or Signup to reply.
  2. PHP able to work with CLI or .php script files otherwise doesn’t work. Because before from run PHP need compile.

    You can use .htaccess abilities instead write code in HTML files. Just like the following code sample.

    RewriteRule ^view.php?mode=prod&id=([0-9]+) /products/$1.html
    

    Creating dynamic URLs in htaccess

    Login or Signup to reply.
  3. Here Moshe Gross’s method works!

    this is only for test purpose:

    i am tryin in xampp:

    .htaccess

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

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="Generator" content="Custa">
    <title></title>
    </head>
    <body>
    <style>p{color:red;font-size:20px;}</style>
        <p>yep yep</p>
    
        <?php
    
        echo 'wasn`t so difficult! '.time(); 
        
        ?>
    </body>
    </html>
    

    when i access http://localhost/testhtmlphp/ i got this result:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="Generator" content="Custa">
    <title></title>
    </head>
    <body>
    <style>p{color:red;font-size:20px;}</style>
        <p>yep yep</p>
    
        wasn`t so difficult! 1656790012</body>
    </html>

    re: if i press F5 (refresh page) the timer change!

    Login or Signup to reply.
  4. Sometimes the .htaccess file doesn’t work on my machine. Try putting it in the virtual host configuration file (/etc/apache2/sites-enabled/[file name].conf)

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

    Tested on Debian 11 64-bit PHP 7.4, Apache 2.4.53

    I don’t know if your hosting will allow that modifications. It probably will work in the .htaccess file.

    Hopefully it helps!

    Credit to Moshe Gross and Constantin

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search