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.
- 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
.htaccessfile of your desired application. If there is no such file, create it;
- Select the
.htaccessfile, 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
.htaccessfile, 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
.htaccessfile, 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
.htaccessfile, 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
.htaccessfile 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.