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
?
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.
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]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With