Process .html Files With PHP

A “handler” is an internal Apache command that tells the Apache server software what action to perform when a file is called. Generally, files have what is know as implicit handlers, which are based on the file type. Normally, all files are simply served by the server, but certain file types are “handled” separately.

For example, PHP files when served to the user are first run through the PHP program running on the server. They are processed and returned back to the Apache server to be sent to the user. The ability to explicitly add handlers to Apache started with version 1.1. This means that handlers can be specifically added to files without regard to the inherent type of file but based on the file extension.

What this means is that you can put PHP or asp code in a .html file and it will be processed by the PHP program on the server before being sent to the user. Why would you want to do that? Well, just think of all the .html or .htm files that you probably currently have on your or some other server you are managing. And just think about all the links to those files. Wouldn't it be nice to just add PHP code to those files and not have to worry about renaming them or changing all the links to them? Sure it would.

So here is how to do it.

Here is the procedure. You need to turn on the ability to see hidden files on the ftp program you use. Then ftp into your site and look for the .htaccess file in the root level of your site. The one that holds your homepage. If one does not exist, then you can create a plan text file that is named “.htaccess”.

Download the file to your hard drive and open it in a text editor that supports UNIX line breaks. On Windows you can use TextPad or on Mac you can use BBEdit Lite or TextWrangler. There may or may not be anything in the file when you open it. On a new line paste in the following.

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

Save the file and then upload it back to your site. That’s it. Your files that end in .html or .htm will now be processed by PHP so you can put PHP code in them.

Author: Daniel Baldwin

Sources: http://httpd.apache.org/docs-2.0/handler.html