Write PHP Code in HTML files
If you have ever come across the scenario in which you have to automate a website in which all the file are having an extension of .html and you have to write PHP code in Html files and you have to change all the file name extensions from .html to .php , After doing this you have to change all the Internal Links of the website too.
The solutions works fine but takes ample amount of time . In some cases it may take hours to do so.
What if I say it can be done in about 20 to 30 seconds and with very less amount of code. COOL RIGHT !!
This is what we are going to do here today and will run PHP Code in Html file or any other desired extension!
So this miracle can be done in two ways .
- The first can be done by working on Configuration of Your Server. (The hard and Delicate one).
- The Second can be done by using .htaccess file. (The easy and Cool one).
Configuring PHP Code in HTML file through Server
First of to do this you need to have access to the configuration of you web server with Apache server configurations also.
Open the Apache Configuration file
For Linux users apache configuration path may be in :
For Windows users apache configuration path may be in :
So what is this file used for ?
This file contains the information about your Apache server: all root settings of your website, all the modules which are loaded etc …
If you are not sure to work with this file do not change any thing because it can crash your server Also take a backup of the file before doing anything .
The line that should be in your interest would be like this:
AddType application/x-httpd-php .phtml .pwml .php4 .php .php2 .inc
As you can see this line gives you the list of files that can go with the PHP Engine
Now we will add the extensions in which want php to work which are (.htm, .html, …)
Now the same line after making the changes:
AddType application/x-httpd-php .phtml .pwml .php4 .php .php2 .inc .htm .html
Now Save this file and restart Apache server and test your .html file containing PHP.
Configuring PHP Code in HTML file through .htaccess file
This part of tutorial applies to you if you do not have access to the server or if you do not want to make changes in the server configuration .
This method may not work with some hosts (basically the free ones)
Now in this .htaccess file we will tell Apache Server that what all extensions we want PHP engine to work with .
So here is the sample code for that :
AddType application/x-httpd-php .NameOfYourExtension
So in our case which is .htm and .html we will write :
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
Now for .htaccess there are two possibilities :
First you already have a .htaccess file in your website root, and therefore you will simply add above lines of code in your file and save it .
Secondly you do not have a .htaccess file in your website root. Now create a htaccess.txt with code above . Now send the file htaccess.txt to website root folder and then rename it to .htaccess .