HomeCoding & Programming

Configure apache server to parse HTML file as PHP using .htaccess

Like Tweet Pin it Share Share Email

How to force apache to parse html files as php?

 

Below are some reasons why people like to parse HTML file as PHP.

  1. Search engines seem to more favor web pages that have .html extension instead of dynamic .php extensions.
  2. Converting an old static website into a dynamic website and worry about losing page ranks.
  3. Security reasons…You don’t want visitors to know what scripting language you using.
 
 
Parsing HTML files as PHP is easily accomplished using .htaccess file.Generally webhost run two versions of PHP as PHP4 & PHP5 usually have a PHP5 handler.
 
 
The below code will parse all .html and .htm files as PHP.
 
AddHandler application/x-httpd-php5 .html .htm
 
 
On most other webhosts
 

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

 

or,if the AddType directive does not work, you can use the AddHandler directive as follows

 

AddHandler application/x-httpd-php .html .htm or
AddHandler x-httpd-php .html .htm

 

Some webhosts, require both directives, as below

 

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

 

You can also try this multi-line approach which uses the SetHandler directive

 

<FilesMatch “\.(html|htm|php)$”>
SetHandler application/x-httpd-php
</FilesMatch>

 

or by

 

<FilesMatch “\.(html|htm|php)$”>
SetHandler application/x-httpd-php5
</FilesMatch>

 

Drawback
Websites using this approach are slower than websites that simply use PHP extension files; So It is a great solution only for small website 🙂

Comments (6)

  • Hello,

    On some small websites I built many years ago, I would like to have the .html pages parsed as .php instead of redirecting all of the existing files to their new .php file names.

    I am a novice programmer but found some advice on how to “Parse HTML Files As PHP using HTACCESS”.

    I am using Dreamhost for hosting some websites and used the following code which worked:

    “AddHandler application/x-httpd-php5 .html .htm”

    The above code is working but my question now is:

    I am adding a wordpress blog to a subfolder and did not want the htaccess code to affect the blog files.

    http://www.maindomain.com/blog/

    How do I exclude the blog files (exclude subfolder) from being affected? I read that the rewrite rule via htaccess works best on smaller websites with a low number of webpages so I do not want the .htaccess to even look at the blog files.

    Thanks for any help!

    Don

    • Author

      Don

      Just put a blank .htaccess file in the folder, you want to exclude.
      The inner .htaccess file will then work instead of outer one.

      Let me know, if you still got the same issue or need some more help.

  • I first want to thank you for the response. I can’t believe I did not see your reply until today!!

    Thanks again…I will try.

    -Don

  • Hello, I check your blogs like every week. Your humoristic style is witty, keep up the good work!

  • Hello, just wanted to mention, I liked this blog post. It was funny. Keep on posting!

Comments are closed.