How To Run PHP Code in HTML files or parse HTML files as PHP

  • Update On January 25th, 2017
  • in Web Development
How To Run PHP Code in HTML files 800x265 - How To Run PHP Code in HTML files or parse HTML files as PHP

This post was last updated on January 25th, 2017 at 06:56 am

Recently I’ve got a client with an old static HTML website (with more than 100 pages) who wants to convert it to a dynamic website to minimize workload, but the condition was not to lose PageRank(PR), so obviously can’t remove the .html extension. It was an impractical and very tedious job to make changes to the site like, if I want to change any menu item, then I’ve to replicate the changes on all the 100 pages.

Fortunately, after lots of searching on the internet, I found that using some codes like below in an HTACCESS file, we can easily run PHP code in HTML files or parse HTML files as PHP on a Linux Apache web server. And it makes my job easier, I separated the common parts (header, sidebar, footer area) and included those parts with the use of PHP includes.

Options +ExecCGI
AddHandler fcgid-script .html
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html

Though the code varies depending on the server configuration but the codes above works with most of the hosting company (GoDaddy, HostGator, and InMotion). Changes to the .htaccess file will affect all the files in the folder including all the sub-folders and sub-files.

Note: If these code samples do not work, it may be necessary to consult your host.

Let’s verify, create a file called php-in-html.html and type some PHP code into it; for example:

<!DOCTYPE html>
<html>
 <body>
    <h1>My First Heading</h1>
    <?php echo "Runing PHP Code in HTML file."; ?>
 </body>
</html>

Upload this HTML page/file to your server and view the page in your favorite browser. If it displays the message then you’re all set.

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you...read more about me

Leave A Reply