Server Side Includes

Server-side includes (SSI) are partway between static HTML files and CGI scripts. A server-side include file looks like HTML; and, in fact, it is. But it also contains some directives inside comments. When the Web server displays the file, it first finds the special directives and acts on them, then displays the resulting file to the browser. It's not nearly as powerful as CGI, but it's not nearly as difficult. There is no security risk, so there is no limitation on which browser can connect to your SSI files. SSI is mostly good for dynamic includes, such as including one file inside another. There are a few other directives; you can display the date a given file was last changed, for example. 

File extensions

All SSI files need to have the file extension .shtml Don't use .html or .htm for SSI. And, conversely, don't use .shtml unless you are actually using SSI capabilities. The server must read the contents of every .shtml file, whether or not you have SSI directives in it.

You probably know that the special name index.html or index.htm is used, if the URL does not include a specific filename. As it turns out, index.shtml is also acceptable, so you can use SSI as the default file in a directory.

Directives

An SSI directive always looks like this:

<!--#element attribute=value attribute=value ... --> 
Note that there is no space between the -- and the # sign. The allowed directives are:
  • include -- used to include one file in another
  • flastmod -- used to print the last time the file was modified
  • fsize -- used to print the file size

The directive exec is not supported. If you want to run CGI, you'll have to run through perlwrap. 

Include content from one file in another file

This example consists of two files, a parent file, where the SSI directive is, and a child file, which gets included in the parent. Try a demo.

Parent file

<html>
<body>
<h1>SSI - Example 1</h1>
<p>I am the parent, and will include a child here:

<!--#include virtual="child.shtml" -->

<p>Now back to the parent.</p>

</body>
</html>

Child file

<p>I am the <i>child</i>.
Note that I may contain HTML markup,
but that I am not normally a fully-valid HTML
file in my own right.
</p>

<p>Note that this child file can contain
SSI directives as well:<br>
<!--#config timefmt="%Y" -->
The year is: <!--#echo var="DATE_LOCAL" -->
This works provided the child file has
an extension of .shtml
</p>

Note that in the SSI directive, the filename of the child is always relative to the parent. You can't use absolute paths. And you can't include files to which you (that is, the web server) do not have read permission.

Display file size or date changed

This example shows how to display the date when a file was last changed. The file whose date is of interest can be any file to which the web server has access. Try a demo.

<html>
<body>
<h1>SSI - Example 2</h1>
<p>
I am example2. Here is the date that I was last changed:

<!--#flastmod virtual="" --> <br />

Here is my size:

<!--#fsize virtual="" --> <br />

And here is the date that example1 was last changed:

<!--#flastmod virtual="example1.shtml" -->

</p>
</body>
</html>
Last updated: 

July 01, 2015