Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 370
Hoon Avatar asked Jul 03 '12 13:07

Hoon


3 Answers

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.

like image 115
John Conde Avatar answered Nov 19 '22 10:11

John Conde


I think writing PHP into an .html file is confusing and anti-natural. Why would you do that??

Anyway, if what you want is to execute PHP files and show them as .html in the address bar, an easiest solution would be using .php as normal, and write a rule in your .htaccess like this:

RewriteRule ^([^.]+)\.html$ $1.php [L]
like image 31
David Morales Avatar answered Nov 19 '22 10:11

David Morales


In order to use php in .html files, you must associate them with your PHP processor in your HTTP server's config file. In Apache, that looks like this:

AddHandler application/x-httpd-php .html
like image 15
Chris Trahey Avatar answered Nov 19 '22 10:11

Chris Trahey