Updated on Sep 29, 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:
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.
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:
.htaccessfile of your desired application. If there is no such file, create it;
.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:
.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.
.htaccessfile, add the line of code to it, do not overwrite it or any other of your settings may stop working.
.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.
.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.
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!