Using PHP or SSI in your HTM Pages

Normally to use server side includes in your page you must rename the page to have a .shtml suffix. Similarly to use PHP in your page it must have a .php suffix. This is because only web pages with these suffixes normally gets processed by the server to interpret these commands before serving the web page.

If you only have one or two pages that use PHP or SSI then this default arrangement is exactly what you want since processing the page on the server slows the display of the page slightly even when there are no PHP or SSI commands in the page to be processed.

The situation is a bit different if you decide that you want to use PHP or SSI in all of your existing pages and don't want to have to change the suffixes on all of your pages names. Firstly, changing all of the page names would mean that you would need to change all of the links in your pages. Secondly, changing the page names would lose all of your search engine placements. Thirdly, links from external sites would no longer work. In this instance we need a way to turn on the appropriate server side processing without changing the existing page names.

Provided that your host allows you to create a .htaccess file on the server you can easily tell the server to process all .htm and/or .html pages for SSI or PHP. The visitors to your page will see ordinary web pages and wont even know that you are using server side processing.

First you need to create a .htaccess file. On Windows this is slightly complicated because the name starts with a dot. Also you may find that a suffix gets added to the name when you save it. You need to make sure that the file is renamed to .htaccess (with the dot on the front and no additional suffix) before you upload it to your server. The unix/linux filename is htaccess with no suffix and the dot on the front means that the file is a hidden file that will not appear in any directory listings.

You need to add the following line to the .htaccess file to tell the server to process all of the .htm and .html for server side includes.

AddHandler server-parsed .htm .html

If you only want to process .htm for server side includes and not .html (or the other way around) then just specify the one that you want to be processed on the server in the command and leave the other one out.

To process PHP commands in your pages requires a different line to be added to your .htaccess file.

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

You can specify whichever combination of these commands that you require to perform the required processing on your pages. If we want to process all of our .htm files for SSI and our .html pages for PHP we could specify:

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

The disadvantage of these commands is that all of our pages will be scanned for server side processing even those that do not contain such commands. This will slow our page loading unnecessarily if most of the pages don't actually require server side processing. On the other hand if we do want to perform server side processing on all of our pages then this is the quickest way to convert all of our pages and make the server side processing completely invisible to your visitors.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow