“SSI is certainly not a replacement for CGI, or other technologies used for generating dynamic web pages. But it is a great way to add small amounts of dynamic content to pages, without doing a lot of extra work.” – the conclusion from “Apache Tutorial: Introduction to Server Side Includes”
Server Side Includes (henceforth SSI) are a part of (that is, built into) Apache, but are not enabled by default in a normal installation. That’s something you have to do. Fortunately it’s not difficult. I’ll show you how to enable it first, then talk about exactly what SSIs are and how you might use them.
To enable SSI, open your main configuration file (httpd.conf) and find the second < Directory… instance – probably somewhere around line 178. Down about 10 lines you’ll see the “Options” entry (it should look something like: Options Indexes FollowSymLinks. To that line you’ll want to add the word “Includes“, making the line look like: Options Indexes FollowSymLinks Includes
Then you need to scroll down to approximately line 211, where you’ll find a statement that looks like this:
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
You’ll need to add the words “index.shtml” between “DirectoryIndex” and “index.html“. This tells Apache which file to serve up to the browser when it asks for your site – in order; that is, the .shtml file first (if there is one) or the .html file if there is no .shtml file. You should finish up with something that looks like this:
<IfModule dir_module>
DirectoryIndex index.shtml index.html
</IfModule>
Next, find the line that says: # To parse .shtml files for server-side includes (SSI):. It’s near the bottom of the <IfModule mime_module>, probably near line 402. Below that you’ll see two commented-out lines that look like this:
#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml
You want to remove the pound signs (un-comment the lines) so they look like this:
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
Okay, you’re finished: save your file, reset Apache Server and SSIs should be enabled. To test this, you can type this into the body of your index.shtml file: “<p> Today’s date is:</span> <!–#echo var=”DATE_LOCAL” –></p>“ When you refresh your index file, it should print out your local day, date and time. If that happens, SSI is properly enabled on your new server.
“Mmm-kay!” you might be saying about now. “It’s nice that they’re enabled and working, but what the heck are SSIs, exactly, and why should I care?” Well, what an SSI is, is a little mini-program that can add information to your webpage – dynamically; that is, the little mini-program generates the information, not your webpage.
Go to “Apache – Server Side Includes” for a complete description of the limited number (they show 10) of SSI elements.
By far the most important (to me, at any rate) is the “include” element. It allows you you dump the contents of a file anywhere you place the “include”.
Most of your pages will will probably contain three (or more) examples: a Header, a Footer and a Menu – usually. The header, for instance, is placed by the use of the statement:<!–#include virtual=”/includes/header.txt”–>
You’ll have the snippet of code that creates the header stored in a plain old text file (header.txt) in the folder “includes” (which you will have created created in the “httpdocs” folder). This little “<!–#include…” statement replaces itself with the contents of header.txt, You’ll place similar include statements for the menu and footer. That sure makes life easier when working on a growing project. As you add menu items, for instance, you’ll only have one file to change/update instead of having to change every page on your website.
Like most things, there is more than one way to do things – in this case, the extension you use to store your includes files. Some people like to store the files with a .ssi extension (for obvious reasons), other folks like to use a .html extension because most of the snippets are html code. The only thing I found that was important was that you should use a .shtml extension if you are going to use SSI inside the snippet.
My downfall was a footer I called using the include statement – “<!–#include virtual=”/includes/footer.shtml–>” – and inside the footer file I called for an SSI variable to display the “last modified” date. I first tried calling the “footer” file with a .txt extension and the SSI variable wouldn’t display. Renaming the file from “footer.txt” to “footer.shtml” allowed the file to use the SSI variable and everything worked just fine. (I wouldn’t want to tell you how long it took me to figure that out…)
An addendum for the WordPress edition of this article: SSI works in HTML/CSS, but not in php, which is what WP is written in, so I can’t show a real life example.