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

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 cPanel → File 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:

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:

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

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

  • 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:

  • 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:

AddHandler application/x-httpd-php56 .php .php5 .html .htm

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

AddHandler application/x-httpd-php70 .php .php5 .html .htm

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

AddHandler application/x-httpd-php71 .php .php5 .html .htm

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

AddHandler application/x-httpd-php72 .php .php5 .html .htm

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

AddHandler application/x-httpd-php73 .php .php5 .html .htm

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

AddHandler application/x-httpd-php74 .php .php5 .html .htm

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.

Was this answer helpful?

Related Articles

How to change PHP timezone for your website

If you have noticed that the correct time is not displaying in your PHP scripts it is because...

How to change my PHP version

We are glad to inform you that you can change the current version of PHP even on shared hosting...

How to change the max memory and max upload limit

Changing of PHP variables is easy on our shared hosting plans. You have full control and you...

Why is phpinfo() Disabled and How to Enable It

The phpinfo() function shows information about server settings and the Kernel version. Such...

How to Set Custom PHP Version per Directory

The PHP version is important not only for speed and performance. There are different...

Powered by WHMCompleteSolution