Sometimes we need to parse HTML or other files as PHP. This can be done using either apache2.conf or httpd.conf whichever applies to your Apache Web Server or by using .htaccess file placed in your root web document directory.
We will discuss .htaccess file as it is the best and easiest way to achieve this feat. I am currently running Ubuntu 14.04 which has the root directory in /var/www/html. Now perform the following steps in order to enable the HTML file processing:
- Create a file by name .htaccess inside the /var/www/html
Write the following inside the .htaccess file
AddType application/x-httpd-php .html .htm
Save the file
Open apache2.conf or httpd.conf and find the section <Directory /var/www/html>
Modify the line AllowOverride None => AllowOverride All
Save the file and restart apache2 webserver
That’s all you are done with it. Now your html or whatever extension files you wish can process the PHP inside it.
3 thoughts on “Enable PHP in HTML or other files”
This is not a soltion,in production we dont have access to apache conf bro!
hey, thank you for the tutorial! I did try what you suggested and I am able to see the PHP code in my HTML file but I can’t see the HTML code anymore. How can I change it to be able to see in an HTML file both HTML and PHP?
Doesn’t work. All that happens on hosts such as GoDaddy.com when the .htaccess file is modified as suggested here is that instead of Web pages being rendered in the browser, the browser tries to download them as binary files. In other words, the Web site stops working.