Apache Guide: Introduction to Server Side Includes

Monday Jun 12th 2000 by Rich Bowen

This is the first of three articles dealing with Server Side Includes, usually called simply SSI. In this article, Rich talks about configuring your server to permit SSI and introduce some basic SSI techniques for adding dynamic content to your existing HTML pages.

This is the first of three articles dealing with Server Side Includes, usually called simply SSI. In this article, I'll talk about configuring your server to permit SSI and introduce some basic SSI techniques for adding dynamic content to your existing HTML pages.

In the second article, we'll talk about some of the somewhat more advanced things you can do with SSI, and in the third week, we'll look at the advanced things that can be done with SSI, such as conditional statements in your SSI directives.

What are SSI?

SSI (Server Side Includes) are directives that are placed in HTML pages and evaluated on the server while the pages are being served. They let you add dynamically generated content to an existing HTML page, without having to serve the entire page via a CGI program or other dynamic technology.

The decision of when to use SSI, and when to have your page entirely generated by some program, is usually a matter of how much of the page is static and how much needs to be recalculated every time the page is served. SSI is a great way to add small pieces of information, such as the current time. But if a majority of your page is being generated at the time that it is served, you need to look for some other solution.

Configuring Your Server to Permit SSI

To permit SSI on your server, you must have the following directive either in your httpd.conf file or in a .htaccess file:

     Options +Includes

This tells Apache that you want to permit files to be parsed for SSI directives.

Not just any file is parsed for SSI directives. You have to tell Apache which files should be parsed. There are two ways to do this. You can tell Apache to parse any file with a particular file extension, such as .shtml, with the following directives:

        AddType text/html .shtml

        AddHandler server-parsed .shtml

One disadvantage to this approach is that if you wanted to add SSI directives to an existing page, you would have to change the name of that page, and all links to that page, in order to give it a .shtml extension, so that those directives would be executed.

The other method is to use the XBitHack directive:

        XBitHack on

XBitHack tells Apache to parse files for SSI directives if they have the execute bit set. So, to add SSI directives to an existing page, rather than having to change the file name, you would just need to make the file executable using chmod.

        chmod +x pagename.html

A brief comment about what not to do. You'll occasionally see people recommending that you just tell Apache to parse all .html files for SSI, so that you don't have to mess with .shtml file names. These folks have perhaps not heard about XBitHack. The thing to keep in mind is that, by doing this, you're requiring that Apache read through every single file that it sends out to clients, even if they don't contain any SSI directives. This can slow things down quite a bit and is not a good idea.

Of course, on Windows, there is no such thing as an execute bit to set, so that limits your options a little if you're running Apache on Windows.

Basic SSI Directives

SSI directives have the following syntax:

        <!--#element attribute=value attribute=value ... -->

It is formatted like an HTML comment, so if you don't have SSI correctly enabled, the browser will ignore it, but it will still be visible in the HTML source. If you have SSI correctly configured, the directive will be replaced with the results of the directive.

The element can be one of a number of things, and we'll talk some more about most of these in the next installment of this series. For now, here are some examples of what you can do with SSI.

Today's Date

        <!--#echo var=DATE_LOCAL -->

The echo element just spits out the value of a variable. There are a number of standard variables, which include the whole set of environment variables that are available to CGI programs. Also, you can define your own variables with the set element.

If you don't like the format in which the date gets printed, you can use the config element, with a timefmt attribute, to modify that formatting.

        <!--#config timefmt="%A %B %d, %Y" -->
        Today is <!--#echo var=DATE_LOCAL -->

Modification Date of the File</4>

        This document last modified <!--#flastmod file="index.html" -->

This element is also subject to timefmt format configurations.

Including the Results of a CGI Program

This is one of the more common uses of SSI - to output the results of a CGI program, such as everybody's favorite, a hit counter.

        <!--#exec cgi="/cgi-bin/counter.pl" -->

We'll definately come back to this in another article.

Other Stuff

And, of course, there are a variety of other things that we can do with SSI. I need to leave something to talk about next week. So, next week, we'll have a lot more examples, and talk about some of the more involved things that you will be able to do with SSI.

Rich Bowen is the Director of Web Application Development at The Creative Group and the author of Apache Server Unleashed.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved