How do I add PHP code/file to HTML(.html) files?

I can’t use PHP in my HTML pages. For example, index.html. I’ve tried using both:

<? contents ?>

and

<?php contents ?>

Neither of these work. My server offers PHP, and when I use a the .php extension, it works properly. Is this a problem or do I have to change the preferences in php.ini?


Answer

You can’t run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:

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

This will tell Apache to process files with a .htm or .html file extension as PHP files.

Leave a Reply

Your email address will not be published. Required fields are marked *