This question already has an answer here:
-
How do I add PHP code/file to HTML(.html) files?
10 answers
I'm working on a project that may have to change the same content on all html pages. So I figured I would create a php file and only have to change that so it changes on all pages over the web.
The files are saved as:
index.html
number.php
EXAMPLE:
------------------------(HTML FILE)----------------------------
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Phone Number</h1>
<?php include('number.php') ?>
</body>
</html>
------------------------(PHP FILE)----------------------------
<?php
echo 4895553268;
?>
What could I do without changing the file extension of all my html's into php. I've found that works but I would like to only change the code in the html page. I've tried include require tags and that didn't work so I tried script tags and can't seem to make it work right.
In order to get the PHP output into the HTML file you need to either
- Change the extension of the HTML to file to PHP and include the PHP from there (simple)
- Load your HTML file into your PHP as a kind of template (a lot of work)
- Change your environment so it deals with HTML as if it was PHP (bad idea)
Create a .htaccess file in directory and add this code to .htaccess file
AddHandler x-httpd-php .html .htm
or
AddType application/x-httpd-php .html .htm
It will force Apache server to parse HTML or HTM files as PHP Script
You would have to configure your webserver to utilize PHP as handler for .html
files. This is typically done by modifying your with AddHandler
to include .html
along with .php
.
Note that this could have a performance impact as this would cause ALL .html files to be run through PHP handler even if there is no PHP involved. So you might strongly consider using .php
extension on these files and adding a redirect as necessary to route requests to specific .html URL's to their .php equivalents.
You'll have to configure the server to interpret .html
files as .php
files. This configuration is different depending on the server software. This will also add an extra step to the server and will slow down response on all your pages and is probably not ideal.