1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <manualpage metafile="ssi.xml.meta">
5 <relativepath href=".."/>
6 <parentdocument href="./">How-To / Tutorials</parentdocument>
8 <title>Apache Tutorial: Introduction to Server Side Includes</title>
11 <p>Server-side includes provide a means to add dynamic content to
12 existing HTML documents.</p>
15 <section id="related"><title>Introduction</title>
18 <module>mod_include</module>
19 <module>mod_cgi</module>
20 <module>mod_expires</module>
24 <directive module="core">Options</directive>
25 <directive module="mod_include">XBitHack</directive>
26 <directive module="mod_mime">AddType</directive>
27 <directive module="core">SetOutputFilter</directive>
28 <directive module="mod_setenvif">BrowserMatchNoCase</directive>
32 <p>This article deals with Server Side Includes, usually called
33 simply SSI. In this article, I'll talk about configuring your
34 server to permit SSI, and introduce some basic SSI techniques
35 for adding dynamic content to your existing HTML pages.</p>
37 <p>In the latter part of the article, we'll talk about some of
38 the somewhat more advanced things that can be done with SSI,
39 such as conditional statements in your SSI directives.</p>
43 <section id="what"><title>What are SSI?</title>
45 <p>SSI (Server Side Includes) are directives that are placed in
46 HTML pages, and evaluated on the server while the pages are
47 being served. They let you add dynamically generated content to
48 an existing HTML page, without having to serve the entire page
49 via a CGI program, or other dynamic technology.</p>
51 <p>The decision of when to use SSI, and when to have your page
52 entirely generated by some program, is usually a matter of how
53 much of the page is static, and how much needs to be
54 recalculated every time the page is served. SSI is a great way
55 to add small pieces of information, such as the current time.
56 But if a majority of your page is being generated at the time
57 that it is served, you need to look for some other
61 <section id="configuring">
62 <title>Configuring your server to permit SSI</title>
64 <p>To permit SSI on your server, you must have the following
65 directive either in your <code>httpd.conf</code> file, or in a
66 <code>.htaccess</code> file:</p>
71 <p>This tells Apache that you want to permit files to be parsed
72 for SSI directives. Note that most configurations contain
73 multiple <directive module="core">Options</directive> directives
74 that can override each other. You will probably need to apply the
75 <code>Options</code> to the specific directory where you want SSI
76 enabled in order to assure that it gets evaluated last.</p>
78 <p>Not just any file is parsed for SSI directives. You have to
79 tell Apache which files should be parsed. There are two ways to
80 do this. You can tell Apache to parse any file with a
81 particular file extension, such as <code>.shtml</code>, with
82 the following directives:</p>
84 AddType text/html .shtml<br />
85 AddOutputFilter INCLUDES .shtml
88 <p>One disadvantage to this approach is that if you wanted to
89 add SSI directives to an existing page, you would have to
90 change the name of that page, and all links to that page, in
91 order to give it a <code>.shtml</code> extension, so that those
92 directives would be executed.</p>
94 <p>The other method is to use the <directive
95 module="mod_include">XBitHack</directive> directive:</p>
100 <p><directive module="mod_include">XBitHack</directive>
101 tells Apache to parse files for SSI
102 directives if they have the execute bit set. So, to add SSI
103 directives to an existing page, rather than having to change
104 the file name, you would just need to make the file executable
105 using <code>chmod</code>.</p>
107 chmod +x pagename.html
110 <p>A brief comment about what not to do. You'll occasionally
111 see people recommending that you just tell Apache to parse all
112 <code>.html</code> files for SSI, so that you don't have to
113 mess with <code>.shtml</code> file names. These folks have
114 perhaps not heard about <directive
115 module="mod_include">XBitHack</directive>. The thing to
116 keep in mind is that, by doing this, you're requiring that
117 Apache read through every single file that it sends out to
118 clients, even if they don't contain any SSI directives. This
119 can slow things down quite a bit, and is not a good idea.</p>
121 <p>Of course, on Windows, there is no such thing as an execute
122 bit to set, so that limits your options a little.</p>
124 <p>In its default configuration, Apache does not send the last
125 modified date or content length HTTP headers on SSI pages,
126 because these values are difficult to calculate for dynamic
127 content. This can prevent your document from being cached, and
128 result in slower perceived client performance. There are two
129 ways to solve this:</p>
132 <li>Use the <code>XBitHack Full</code> configuration. This
133 tells Apache to determine the last modified date by looking
134 only at the date of the originally requested file, ignoring
135 the modification date of any included files.</li>
137 <li>Use the directives provided by
138 <module>mod_expires</module> to set an explicit expiration
139 time on your files, thereby letting browsers and proxies
140 know that it is acceptable to cache them.</li>
144 <section id="basic"><title>Basic SSI directives</title>
146 <p>SSI directives have the following syntax:</p>
148 <!--#element attribute=value attribute=value ... -->
151 <p>It is formatted like an HTML comment, so if you don't have
152 SSI correctly enabled, the browser will ignore it, but it will
153 still be visible in the HTML source. If you have SSI correctly
154 configured, the directive will be replaced with its
157 <p>The element can be one of a number of things, and we'll talk
158 some more about most of these in the next installment of this
159 series. For now, here are some examples of what you can do with
162 <section id="todaysdate"><title>Today's date</title>
165 <!--#echo var="DATE_LOCAL" -->
168 <p>The <code>echo</code> element just spits out the value of a
169 variable. There are a number of standard variables, which
170 include the whole set of environment variables that are
171 available to CGI programs. Also, you can define your own
172 variables with the <code>set</code> element.</p>
174 <p>If you don't like the format in which the date gets printed,
175 you can use the <code>config</code> element, with a
176 <code>timefmt</code> attribute, to modify that formatting.</p>
179 <!--#config timefmt="%A %B %d, %Y" --><br />
180 Today is <!--#echo var="DATE_LOCAL" -->
184 <section id="lastmodified"><title>Modification date of the file</title>
187 This document last modified <!--#flastmod file="index.html" -->
190 <p>This element is also subject to <code>timefmt</code> format
194 <section id="cgi"><title>Including the results of a CGI program</title>
196 <p>This is one of the more common uses of SSI - to output the
197 results of a CGI program, such as everybody's favorite, a ``hit
201 <!--#include virtual="/cgi-bin/counter.pl" -->
207 <section id="additionalexamples">
208 <title>Additional examples</title>
210 <p>Following are some specific examples of things you can do in
211 your HTML documents with SSI.</p>
213 <section id="docmodified"><title>When was this document
216 <p>Earlier, we mentioned that you could use SSI to inform the
217 user when the document was most recently modified. However, the
218 actual method for doing that was left somewhat in question. The
219 following code, placed in your HTML document, will put such a
220 time stamp on your page. Of course, you will have to have SSI
221 correctly enabled, as discussed above.</p>
223 <!--#config timefmt="%A %B %d, %Y" --><br />
224 This file last modified <!--#flastmod file="ssi.shtml" -->
227 <p>Of course, you will need to replace the
228 <code>ssi.shtml</code> with the actual name of the file that
229 you're referring to. This can be inconvenient if you're just
230 looking for a generic piece of code that you can paste into any
231 file, so you probably want to use the
232 <code>LAST_MODIFIED</code> variable instead:</p>
234 <!--#config timefmt="%D" --><br />
235 This file last modified <!--#echo var="LAST_MODIFIED" -->
238 <p>For more details on the <code>timefmt</code> format, go to
239 your favorite search site and look for <code>strftime</code>. The
240 syntax is the same.</p>
243 <section id="standard-footer">
244 <title>Including a standard footer</title>
246 <p>If you are managing any site that is more than a few pages,
247 you may find that making changes to all those pages can be a
248 real pain, particularly if you are trying to maintain some kind
249 of standard look across all those pages.</p>
251 <p>Using an include file for a header and/or a footer can
252 reduce the burden of these updates. You just have to make one
253 footer file, and then include it into each page with the
254 <code>include</code> SSI command. The <code>include</code>
255 element can determine what file to include with either the
256 <code>file</code> attribute, or the <code>virtual</code>
257 attribute. The <code>file</code> attribute is a file path,
258 <em>relative to the current directory</em>. That means that it
259 cannot be an absolute file path (starting with /), nor can it
260 contain ../ as part of that path. The <code>virtual</code>
261 attribute is probably more useful, and should specify a URL
262 relative to the document being served. It can start with a /,
263 but must be on the same server as the file being served.</p>
265 <!--#include virtual="/footer.html" -->
268 <p>I'll frequently combine the last two things, putting a
269 <code>LAST_MODIFIED</code> directive inside a footer file to be
270 included. SSI directives can be contained in the included file,
271 and includes can be nested - that is, the included file can
272 include another file, and so on.</p>
277 <section id="config">
278 <title>What else can I config?</title>
280 <p>In addition to being able to <code>config</code> the time
281 format, you can also <code>config</code> two other things.</p>
283 <p>Usually, when something goes wrong with your SSI directive,
284 you get the message</p>
286 [an error occurred while processing this directive]
289 <p>If you want to change that message to something else, you
290 can do so with the <code>errmsg</code> attribute to the
291 <code>config</code> element:</p>
293 <!--#config errmsg="[It appears that you don't know how to use SSI]" -->
296 <p>Hopefully, end users will never see this message, because
297 you will have resolved all the problems with your SSI
298 directives before your site goes live. (Right?)</p>
300 <p>And you can <code>config</code> the format in which file
301 sizes are returned with the <code>sizefmt</code> attribute. You
302 can specify <code>bytes</code> for a full count in bytes, or
303 <code>abbrev</code> for an abbreviated number in Kb or Mb, as
308 <title>Executing commands</title>
310 <p>I expect that I'll have an article some time in the coming
311 months about using SSI with small CGI programs. For now, here's
312 something else that you can do with the <code>exec</code>
313 element. You can actually have SSI execute a command using the
314 shell (<code>/bin/sh</code>, to be precise - or the DOS shell,
315 if you're on Win32). The following, for example, will give you
316 a directory listing.</p>
319 <!--#exec cmd="ls" --><br />
323 <p>or, on Windows</p>
326 <!--#exec cmd="dir" --><br />
330 <p>You might notice some strange formatting with this directive
331 on Windows, because the output from <code>dir</code> contains
332 the string ``<<code>dir</code>>'' in it, which confuses
335 <p>Note that this feature is exceedingly dangerous, as it will
336 execute whatever code happens to be embedded in the
337 <code>exec</code> tag. If you have any situation where users
338 can edit content on your web pages, such as with a
339 ``guestbook'', for example, make sure that you have this
340 feature disabled. You can allow SSI, but not the
341 <code>exec</code> feature, with the <code>IncludesNOEXEC</code>
342 argument to the <code>Options</code> directive.</p>
345 <section id="advanced">
346 <title>Advanced SSI techniques</title>
348 <p>In addition to spitting out content, Apache SSI gives you
349 the option of setting variables, and using those variables in
350 comparisons and conditionals.</p>
352 <section id="caveat"><title>Caveat</title>
354 <p>Most of the features discussed in this article are only
355 available to you if you are running Apache 1.2 or later. Of
356 course, if you are not running Apache 1.2 or later, you need to
357 upgrade immediately, if not sooner. Go on. Do it now. We'll
361 <section id="variables"><title>Setting variables</title>
363 <p>Using the <code>set</code> directive, you can set variables
364 for later use. We'll need this later in the discussion, so
365 we'll talk about it here. The syntax of this is as follows:</p>
367 <!--#set var="name" value="Rich" -->
370 <p>In addition to merely setting values literally like that,
371 you can use any other variable, including, for example,
372 environment variables, or some of the variables we discussed in
373 the last article (like <code>LAST_MODIFIED</code>, for example)
374 to give values to your variables. You will specify that
375 something is a variable, rather than a literal string, by using
376 the dollar sign ($) before the name of the variable.</p>
378 <!--#set var="modified" value="$LAST_MODIFIED" -->
381 <p>To put a literal dollar sign into the value of your
382 variable, you need to escape the dollar sign with a
385 <!--#set var="cost" value="\$100" -->
388 <p>Finally, if you want to put a variable in the midst of a
389 longer string, and there's a chance that the name of the
390 variable will run up against some other characters, and thus be
391 confused with those characters, you can place the name of the
392 variable in braces, to remove this confusion. (It's hard to
393 come up with a really good example of this, but hopefully
394 you'll get the point.)</p>
396 <!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" -->
400 <section id="conditional">
401 <title>Conditional expressions</title>
403 <p>Now that we have variables, and are able to set and compare
404 their values, we can use them to express conditionals. This
405 lets SSI be a tiny programming language of sorts.
406 <module>mod_include</module> provides an <code>if</code>,
407 <code>elif</code>, <code>else</code>, <code>endif</code>
408 structure for building conditional statements. This allows you
409 to effectively generate multiple logical pages out of one
412 <p>The structure of this conditional construct is:</p>
414 <!--#if expr="test_condition" --><br />
415 <!--#elif expr="test_condition" --><br />
416 <!--#else --><br />
420 <p>A <em>test_condition</em> can be any sort of logical
421 comparison - either comparing values to one another, or testing
422 the ``truth'' of a particular value. (A given string is true if
423 it is nonempty.) For a full list of the comparison operators
424 available to you, see the <module>mod_include</module>
425 documentation. Here are some examples of how one might use this
428 <p>In your configuration file, you could put the following
431 BrowserMatchNoCase macintosh Mac<br />
432 BrowserMatchNoCase MSIE InternetExplorer
435 <p>This will set environment variables ``Mac'' and
436 ``InternetExplorer'' to true, if the client is running Internet
437 Explorer on a Macintosh.</p>
439 <p>Then, in your SSI-enabled document, you might do the
442 <!--#if expr="${Mac} && ${InternetExplorer}" --><br />
443 Apologetic text goes here<br />
444 <!--#else --><br />
445 Cool JavaScript code goes here<br />
449 <p>Not that I have anything against IE on Macs - I just
450 struggled for a few hours last week trying to get some
451 JavaScript working on IE on a Mac, when it was working
452 everywhere else. The above was the interim workaround.</p>
454 <p>Any other variable (either ones that you define, or normal
455 environment variables) can be used in conditional statements.
456 With Apache's ability to set environment variables with the
457 <code>SetEnvIf</code> directives, and other related directives,
458 this functionality can let you do some pretty involved dynamic
459 stuff without ever resorting to CGI.</p>
463 <section id="conclusion"><title>Conclusion</title>
465 <p>SSI is certainly not a replacement for CGI, or other
466 technologies used for generating dynamic web pages. But it is a
467 great way to add small amounts of dynamic content to pages,
468 without doing a lot of extra work.</p>