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:

  1. Create a file by name .htaccess inside the /var/www/html

  2. Write the following inside the .htaccess file

AddType application/x-httpd-php .html .htm

  1. Save the file

  2. Open apache2.conf or httpd.conf and find the section <Directory /var/www/html>

  3. Modify the line AllowOverride None  => AllowOverride All

  4. 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.

 

Enable PHP in HTML or other files

3 thoughts on “Enable PHP in HTML or other files

  • December 19, 2017 at 5:58 pm
    Permalink

    This is not a soltion,in production we dont have access to apache conf bro!

    Reply
  • January 20, 2018 at 3:15 pm
    Permalink

    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?

    Reply
  • May 29, 2018 at 2:46 pm
    Permalink

    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.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.