PHP into .html file
If you need to add php code into .html (or .htm) file and you don’t want to change the file extension to .php because you already have incoming links on the .html file - although you can solve that with a simple redirect - here is the solution:
Add the following lines of code to your .htaccess file:
AddType application/x-httpd-php .html
If you just need one .html file to behave like that, then enclose that code with “files” tag like this:
AddType application/x-httpd-php .html
Pretty simple, right?
Here is what’s happening - when a web page is requested, the server checks the file extension to know how to handle the page. If it sees a .php extension (or .shtml, .asp etc.), it knows that it needs to execute the appropriate code before passing it along to the browser. If it sees a .htm or .html file, it sends it right to the browser because it doesn’t have anything to process on the server.
With the above piece of code, we simply tell to server to execute a .html file as if it’s a .php one.
- If you have an existing .htaccess file, make sure not to overwrite anything there or other settings may stop working!
- Anything in your .html files that starts with <? will now be executed as PHP, so if it’s in your file for some other reason (an XML tag for example) you will need to echo these lines to prevent errors. For example:
<?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
Be aware!
After you made the changes to .htaccess file, create a test file and copy the following html into it:
<html>
<head></head>
<body>
<h1>
<?php echo "PHP code detected !!!"; ?>
</h1>
</body>
</html>
Upload it to your web server and view it using your browser. You will see that it works just fine.
Tags:PHP , programming stuffPopularity: 27% [?]






For somebody who is only getting started like me that is an excellent tip and thank you very much for posting it.