Execute .html pages in All .php files Using .htaccess: A Complete Guide

Execute .html pages in All .php files Using .htaccess: A Complete Guide

Many web developers and administrators alike use PHP files in conjunction with HTML. This opens up many opportunities to enhance the features and functionalities. Even if you’re not going to create dynamic pages for your website, doing so will help in other areas like server-side rendering.

In this article, learn how you can make all .html pages parse PHP codes by editing the .htaccess file.

Is it Worth Running PHP in HTML?

When you create a website, the basic files are either in .html or .php. HTML is referred to as the skeleton for the web. It’s a Markup language that displays content on a web browser. HTML is not processed on the server-side and is rendered in a web browser.

PHP, on the other hand, is processed and served on the browser from the server. PHP files can read and process HTML codes, but it’s not the other way around.

You can parse HTML files to run as PHP. Here are some of the benefits to this approach:
  • It allows you to covert old static websites to dynamic ones. The best way is to create .PHP files from scratch. But at times, it’s not possible for existing HTML pages since they may disrupt JavaScript files and disrupt old links. To get around this, developers parse HTML web pages as PHP
  • You can add functionalities to the old HTML web pages. You can use PHP scripting to include server-side processing.
  • HTML helps you rank better in search engines. So some element of HTML within PHP is better.

Parsing PHP in HTML Files

To parse PHP in HTML files, you need to add handlers to the .htaccess file. It is a configuration file that you can use to change the existing configuration of your website. In case you don’t have an .htaccess file to edit, you have to create one. You can do so by going into cPanel > File Manager. Create a file with “.htaccess” extension. Now you’ll be able to include PHP scripts along with HTML.

Before moving ahead, learn which PHP version you’re currently running. For this, log into your cPanel and go into “Select PHP Version” under Software. It will show you the PHP version you’re on.

Now access the .htaccess file. Open it and add the following code:

Code:
AddHandler application/x-httpd-phpV .php .php5 .html .htm
Replace “V” with the PHP version. So if you’re using PHP 5.6, the code would look like:

Code:
AddHandler application/x-httpd-php56 .php .php5 .html .htm
There should be no “.”(dot) in between 5 and 6.

Now the HTML file will be processed like a PHP file. Embed PHP codes in the HTML file to verify that you’ve successfully made the changes.

Please note that you should make the changes only for small websites with less than 50 pages in it. For bigger websites, it can slow the performance.

Also, if you’re moving to a new server that has an interface other than cPanel, you should take necessary measures. You run the risk of exposing your PHP codes to website visitors. This is a critical mistake.

For further assistance, contact your hosting provider.
Author
kumkumsharma
Views
5,891
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top