How to Make All .html Pages to Run as .php Files Using .htaccess

Updated on May 22, 2020

PHP can be extremely useful when used in conjunction with HTML to enhance the features of a website. You find the perfect script, and you want to run it on your website, but you need to include PHP on your page for it to work. Even if you don’t plan to create full-blown dynamic pages certain functions like server-side can help you repeat the code less for commonly used things like menus and advertisements, or perform tasks such as verifying reCaptcha attempts using the current API. Modifying each web page manually to add PHP code is a little impractical and monotonous task.

In this post, we will share how to make .html files be parsed as .php files, as well as various reasons why you may want to do that.

You will find:

Why Run PHP in HTML Files?

When you decide to create your own website, there are many options for you. The basic files are generally either .html files (or .htm), or .PHP files. HTML is the markup language for displaying content and is not processed on the server-side. PHP is a programming style language used to create pages that are processed and served from the server. PHP files can always read and display HTML code, but that does not work the other way around. 

Parsing is the process of analyzing the code in a particular file and executing it the way it should be executed. There are many reasons to parse HTML files to run as PHP instead. 

  • Some webmasters do it because they are required to convert an old static website into a dynamic one. As a best practice for such cases would be to create new pages as .PHP files so that they are built from the beginning to be able to use PHP. However, sometimes this is not possible for existing pages as it can have several adverse effects. Handling such a change is a delicate exercise as changing the file extension can break Javascript, and even disrupt old links that directly reference with the .HTM or .HTML files. To get rid of this exercise, webmasters can parse HTML web pages as PHP.
  • There are times wherein you are required to parse HTML web pages as PHP. Let's say, you have older HTML web pages and you are required to add some additional functionality. One of the best and easiest ways to include server-side processing in your web pages is with PHP scripting. For example, you can add a log-in screen or a survey, redirect visitors, create a calendar, send and receive cookies, and more. In such a case, you would not need to manually modify each web page and add the PHP code, but only parse the needed HTML web pages as PHP using .htacccess file.
  • There is a common polemic in the SEO world, that pages with a .HTML extension ranks more in search engines when compared to .PHP. According to Google’s John Mueller though, Google doesn’t care whether URLs end in .HTML, .PHP, .ASP, or have no file extensions at all, as good SEO practices are server-side language-agnostic anyway.
  • Others prefer to perform this for security reasons.

How to Parse PHP in HTML Files?

To make a .html file process as a .PHP file, you have to make adjustments by adding a handler to your .htaccess file. If the .htaccess file is not available for you to edit, you can create a new one via your cPanelFile Manager. You could also create a text file named .htaccess on your local computer, fill the required lines of code and then upload the file with your favorite FTP client to the server on which your website is hosted.

If you’ve never seen or heard of a .htaccess file before, don’t be scared. Htaccess files are simple text files that are saved with a .htaccess extension. And they are easily created using a simple text editor such as Notepad or WordPad.

Once you do that, you will be able to include PHP scripts with your existing HTML that will be executed server-side when your site is loaded.

Make sure to create a backup of your original .htaccess file, as making changes to this file can disrupt access to your website.

Before you head on to perform that change you need to know which version of PHP is running on your account. In order to acquire this information you should follow these steps:

Choose the Select PHP Version icon from the cPanel Software section:
Find PHP Selector in cPanel

After you are sure which PHP version is currently running on your account you should access the .htaccess file of your application which you can perform easily via:

  • Access your cPanel account main dashboard;
  • Locate the File Manager icon from the Files section:

Find File Manager in cPanel for WordPress Site

  • Once inside the File Manager, click on Settings at the top-right corner, and then enable Show Hidden Files (dotfiles):

Show Hidden Files in File Manager

  • After you do that, locate the .htaccess file of your desired application. If there is no such file, create it;

Create a New htaccess File

  • Select the .htaccess file, and click the right mouse button over it. Select Edit, and after you get a pop-up window, click on the button Edit again to confirm:

Click Edit on Htaccess for via File Manager

  • Once inside the .htaccess file, you will be able to perform changes on it. Depending on the current PHP version, which you have already checked you should apply one and only one of the following lines at the top of the file:

Depending on your PHP version, add one of the following lines at the beginning of the file:

# If you are using PHP 5.6, add the following line:

AddType application/x-lsphp53 .php7 .php5 .php4 .php .php3 .php2 .phtml

# If you are using PHP 7.0, add the following line:

AddType application/x-lsphp70 .php7 .php5 .php4 .php .php3 .php2 .phtml

# If you are using PHP 7.1, add the following line:

AddType application/x-lsphp71 .php7 .php5 .php4 .php .php3 .php2 .phtml

# If you are using PHP 7.2, add the following line:

AddType application/x-lsphp72 .php7 .php5 .php4 .php .php3 .php2 .phtml

# If you are using PHP 7.3, add the following line:

AddType application/x-lsphp73 .php7 .php5 .php4 .php .php3 .php2 .phtml

# If you are using PHP 7.4, add the following line:

AddType application/x-lsphp74 .php7 .php5 .php4 .php .php3 .php2 .phtml

This will cause the HTML file to be processed as if it were PHP. Now you can embed your PHP code in the HTML file and it should get processed correctly.

Security & Performance Complications when .HTML Files are Processed as .PHP Files

  • If you have an already existing .htaccess file, add the line of code to it, do not overwrite it or any other of your settings may stop working.
  • If you are new to working with your .htaccess file, it is a common cause to make a mistake when creating it. The most frequent mistake is to save the file using the wrong extension.
  • If you move the account to a different server, for example, one that doesn't use cPanel, your .htaccess file may not be compatible with the new configuration. Ultimately, you run a real risk of exposing your PHP code to regular website visitors. If that PHP code has sensitive data such as passwords or database information, then that could be a disaster. Note that this is not an inherently wrong action in case you have a specific reason for needing it. But you should be aware to use it with caution.
  • This method is recommended for small websites (those with less than 50 pages) as it can slow the performance of the website. As your website increases in size, it would be a good idea to select a more robust solution that will be able to serve a website of any size.

If you need any help, you can always count on our technical support team. Open a support ticket, explain what you need assistance with, and our experts will make sure to help you as quickly as possible.

We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!

SSD Cloud Hosting

  • Free Domain Transfer
  • 24/7 Technical Support
  • Fast SSD Storage
  • Hack-free Protection
  • Free Script Installation
  • Free Website Transfer
  • Free Cloudflare CDN
  • Immediate Activation
View More