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 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="ssi.xml.meta">
24 <parentdocument href="./">How-To / Tutorials</parentdocument>
26 <title>Apache Tutorial: Introduction to Server Side Includes</title>
29 <p>Server-side includes provide a means to add dynamic content to
30 existing HTML documents.</p>
33 <section id="related"><title>Introduction</title>
36 <module>mod_include</module>
37 <module>mod_cgi</module>
38 <module>mod_expires</module>
42 <directive module="core">Options</directive>
43 <directive module="mod_include">XBitHack</directive>
44 <directive module="mod_mime">AddType</directive>
45 <directive module="core">SetOutputFilter</directive>
46 <directive module="mod_setenvif">BrowserMatchNoCase</directive>
50 <p>This article deals with Server Side Includes, usually called
51 simply SSI. In this article, I'll talk about configuring your
52 server to permit SSI, and introduce some basic SSI techniques
53 for adding dynamic content to your existing HTML pages.</p>
55 <p>In the latter part of the article, we'll talk about some of
56 the somewhat more advanced things that can be done with SSI,
57 such as conditional statements in your SSI directives.</p>
61 <section id="what"><title>What are SSI?</title>
63 <p>SSI (Server Side Includes) are directives that are placed in
64 HTML pages, and evaluated on the server while the pages are
65 being served. They let you add dynamically generated content to
66 an existing HTML page, without having to serve the entire page
67 via a CGI program, or other dynamic technology.</p>
69 <p>The decision of when to use SSI, and when to have your page
70 entirely generated by some program, is usually a matter of how
71 much of the page is static, and how much needs to be
72 recalculated every time the page is served. SSI is a great way
73 to add small pieces of information, such as the current time.
74 But if a majority of your page is being generated at the time
75 that it is served, you need to look for some other
79 <section id="configuring">
80 <title>Configuring your server to permit SSI</title>
82 <p>To permit SSI on your server, you must have the following
83 directive either in your <code>httpd.conf</code> file, or in a
84 <code>.htaccess</code> file:</p>
89 <p>This tells Apache that you want to permit files to be parsed
90 for SSI directives. Note that most configurations contain
91 multiple <directive module="core">Options</directive> directives
92 that can override each other. You will probably need to apply the
93 <code>Options</code> to the specific directory where you want SSI
94 enabled in order to assure that it gets evaluated last.</p>
96 <p>Not just any file is parsed for SSI directives. You have to
97 tell Apache which files should be parsed. There are two ways to
98 do this. You can tell Apache to parse any file with a
99 particular file extension, such as <code>.shtml</code>, with
100 the following directives:</p>
102 AddType text/html .shtml<br />
103 AddOutputFilter INCLUDES .shtml
106 <p>One disadvantage to this approach is that if you wanted to
107 add SSI directives to an existing page, you would have to
108 change the name of that page, and all links to that page, in
109 order to give it a <code>.shtml</code> extension, so that those
110 directives would be executed.</p>
112 <p>The other method is to use the <directive
113 module="mod_include">XBitHack</directive> directive:</p>
118 <p><directive module="mod_include">XBitHack</directive>
119 tells Apache to parse files for SSI
120 directives if they have the execute bit set. So, to add SSI
121 directives to an existing page, rather than having to change
122 the file name, you would just need to make the file executable
123 using <code>chmod</code>.</p>
125 chmod +x pagename.html
128 <p>A brief comment about what not to do. You'll occasionally
129 see people recommending that you just tell Apache to parse all
130 <code>.html</code> files for SSI, so that you don't have to
131 mess with <code>.shtml</code> file names. These folks have
132 perhaps not heard about <directive
133 module="mod_include">XBitHack</directive>. The thing to
134 keep in mind is that, by doing this, you're requiring that
135 Apache read through every single file that it sends out to
136 clients, even if they don't contain any SSI directives. This
137 can slow things down quite a bit, and is not a good idea.</p>
139 <p>Of course, on Windows, there is no such thing as an execute
140 bit to set, so that limits your options a little.</p>
142 <p>In its default configuration, Apache does not send the last
143 modified date or content length HTTP headers on SSI pages,
144 because these values are difficult to calculate for dynamic
145 content. This can prevent your document from being cached, and
146 result in slower perceived client performance. There are two
147 ways to solve this:</p>
150 <li>Use the <code>XBitHack Full</code> configuration. This
151 tells Apache to determine the last modified date by looking
152 only at the date of the originally requested file, ignoring
153 the modification date of any included files.</li>
155 <li>Use the directives provided by
156 <module>mod_expires</module> to set an explicit expiration
157 time on your files, thereby letting browsers and proxies
158 know that it is acceptable to cache them.</li>
162 <section id="basic"><title>Basic SSI directives</title>
164 <p>SSI directives have the following syntax:</p>
166 <!--#element attribute=value attribute=value ... -->
169 <p>It is formatted like an HTML comment, so if you don't have
170 SSI correctly enabled, the browser will ignore it, but it will
171 still be visible in the HTML source. If you have SSI correctly
172 configured, the directive will be replaced with its
175 <p>The element can be one of a number of things, and we'll talk
176 some more about most of these in the next installment of this
177 series. For now, here are some examples of what you can do with
180 <section id="todaysdate"><title>Today's date</title>
183 <!--#echo var="DATE_LOCAL" -->
186 <p>The <code>echo</code> element just spits out the value of a
187 variable. There are a number of standard variables, which
188 include the whole set of environment variables that are
189 available to CGI programs. Also, you can define your own
190 variables with the <code>set</code> element.</p>
192 <p>If you don't like the format in which the date gets printed,
193 you can use the <code>config</code> element, with a
194 <code>timefmt</code> attribute, to modify that formatting.</p>
197 <!--#config timefmt="%A %B %d, %Y" --><br />
198 Today is <!--#echo var="DATE_LOCAL" -->
202 <section id="lastmodified"><title>Modification date of the file</title>
205 This document last modified <!--#flastmod file="index.html" -->
208 <p>This element is also subject to <code>timefmt</code> format
212 <section id="cgi"><title>Including the results of a CGI program</title>
214 <p>This is one of the more common uses of SSI - to output the
215 results of a CGI program, such as everybody's favorite, a ``hit
219 <!--#include virtual="/cgi-bin/counter.pl" -->
225 <section id="additionalexamples">
226 <title>Additional examples</title>
228 <p>Following are some specific examples of things you can do in
229 your HTML documents with SSI.</p>
231 <section id="docmodified"><title>When was this document
234 <p>Earlier, we mentioned that you could use SSI to inform the
235 user when the document was most recently modified. However, the
236 actual method for doing that was left somewhat in question. The
237 following code, placed in your HTML document, will put such a
238 time stamp on your page. Of course, you will have to have SSI
239 correctly enabled, as discussed above.</p>
241 <!--#config timefmt="%A %B %d, %Y" --><br />
242 This file last modified <!--#flastmod file="ssi.shtml" -->
245 <p>Of course, you will need to replace the
246 <code>ssi.shtml</code> with the actual name of the file that
247 you're referring to. This can be inconvenient if you're just
248 looking for a generic piece of code that you can paste into any
249 file, so you probably want to use the
250 <code>LAST_MODIFIED</code> variable instead:</p>
252 <!--#config timefmt="%D" --><br />
253 This file last modified <!--#echo var="LAST_MODIFIED" -->
256 <p>For more details on the <code>timefmt</code> format, go to
257 your favorite search site and look for <code>strftime</code>. The
258 syntax is the same.</p>
261 <section id="standard-footer">
262 <title>Including a standard footer</title>
264 <p>If you are managing any site that is more than a few pages,
265 you may find that making changes to all those pages can be a
266 real pain, particularly if you are trying to maintain some kind
267 of standard look across all those pages.</p>
269 <p>Using an include file for a header and/or a footer can
270 reduce the burden of these updates. You just have to make one
271 footer file, and then include it into each page with the
272 <code>include</code> SSI command. The <code>include</code>
273 element can determine what file to include with either the
274 <code>file</code> attribute, or the <code>virtual</code>
275 attribute. The <code>file</code> attribute is a file path,
276 <em>relative to the current directory</em>. That means that it
277 cannot be an absolute file path (starting with /), nor can it
278 contain ../ as part of that path. The <code>virtual</code>
279 attribute is probably more useful, and should specify a URL
280 relative to the document being served. It can start with a /,
281 but must be on the same server as the file being served.</p>
283 <!--#include virtual="/footer.html" -->
286 <p>I'll frequently combine the last two things, putting a
287 <code>LAST_MODIFIED</code> directive inside a footer file to be
288 included. SSI directives can be contained in the included file,
289 and includes can be nested - that is, the included file can
290 include another file, and so on.</p>
295 <section id="config">
296 <title>What else can I config?</title>
298 <p>In addition to being able to <code>config</code> the time
299 format, you can also <code>config</code> two other things.</p>
301 <p>Usually, when something goes wrong with your SSI directive,
302 you get the message</p>
304 [an error occurred while processing this directive]
307 <p>If you want to change that message to something else, you
308 can do so with the <code>errmsg</code> attribute to the
309 <code>config</code> element:</p>
311 <!--#config errmsg="[It appears that you don't know how to use SSI]" -->
314 <p>Hopefully, end users will never see this message, because
315 you will have resolved all the problems with your SSI
316 directives before your site goes live. (Right?)</p>
318 <p>And you can <code>config</code> the format in which file
319 sizes are returned with the <code>sizefmt</code> attribute. You
320 can specify <code>bytes</code> for a full count in bytes, or
321 <code>abbrev</code> for an abbreviated number in Kb or Mb, as
326 <title>Executing commands</title>
328 <p>I expect that I'll have an article some time in the coming
329 months about using SSI with small CGI programs. For now, here's
330 something else that you can do with the <code>exec</code>
331 element. You can actually have SSI execute a command using the
332 shell (<code>/bin/sh</code>, to be precise - or the DOS shell,
333 if you're on Win32). The following, for example, will give you
334 a directory listing.</p>
337 <!--#exec cmd="ls" --><br />
341 <p>or, on Windows</p>
344 <!--#exec cmd="dir" --><br />
348 <p>You might notice some strange formatting with this directive
349 on Windows, because the output from <code>dir</code> contains
350 the string ``<<code>dir</code>>'' in it, which confuses
353 <p>Note that this feature is exceedingly dangerous, as it will
354 execute whatever code happens to be embedded in the
355 <code>exec</code> tag. If you have any situation where users
356 can edit content on your web pages, such as with a
357 ``guestbook'', for example, make sure that you have this
358 feature disabled. You can allow SSI, but not the
359 <code>exec</code> feature, with the <code>IncludesNOEXEC</code>
360 argument to the <code>Options</code> directive.</p>
363 <section id="advanced">
364 <title>Advanced SSI techniques</title>
366 <p>In addition to spitting out content, Apache SSI gives you
367 the option of setting variables, and using those variables in
368 comparisons and conditionals.</p>
370 <section id="variables"><title>Setting variables</title>
372 <p>Using the <code>set</code> directive, you can set variables
373 for later use. We'll need this later in the discussion, so
374 we'll talk about it here. The syntax of this is as follows:</p>
376 <!--#set var="name" value="Rich" -->
379 <p>In addition to merely setting values literally like that, you
380 can use any other variable, including <a
381 href="../env.html">environment variables</a> or the variables
382 discussed above (like <code>LAST_MODIFIED</code>, for example) to
383 give values to your variables. You will specify that something is
384 a variable, rather than a literal string, by using the dollar sign
385 ($) before the name of the variable.</p>
387 <example> <!--#set var="modified" value="$LAST_MODIFIED" -->
390 <p>To put a literal dollar sign into the value of your
391 variable, you need to escape the dollar sign with a
394 <!--#set var="cost" value="\$100" -->
397 <p>Finally, if you want to put a variable in the midst of a
398 longer string, and there's a chance that the name of the
399 variable will run up against some other characters, and thus be
400 confused with those characters, you can place the name of the
401 variable in braces, to remove this confusion. (It's hard to
402 come up with a really good example of this, but hopefully
403 you'll get the point.)</p>
405 <!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" -->
409 <section id="conditional">
410 <title>Conditional expressions</title>
412 <p>Now that we have variables, and are able to set and compare
413 their values, we can use them to express conditionals. This
414 lets SSI be a tiny programming language of sorts.
415 <module>mod_include</module> provides an <code>if</code>,
416 <code>elif</code>, <code>else</code>, <code>endif</code>
417 structure for building conditional statements. This allows you
418 to effectively generate multiple logical pages out of one
421 <p>The structure of this conditional construct is:</p>
423 <!--#if expr="test_condition" --><br />
424 <!--#elif expr="test_condition" --><br />
425 <!--#else --><br />
429 <p>A <em>test_condition</em> can be any sort of logical
430 comparison - either comparing values to one another, or testing
431 the ``truth'' of a particular value. (A given string is true if
432 it is nonempty.) For a full list of the comparison operators
433 available to you, see the <module>mod_include</module>
434 documentation. Here are some examples of how one might use this
437 <p>In your configuration file, you could put the following
440 BrowserMatchNoCase macintosh Mac<br />
441 BrowserMatchNoCase MSIE InternetExplorer
444 <p>This will set environment variables ``Mac'' and
445 ``InternetExplorer'' to true, if the client is running Internet
446 Explorer on a Macintosh.</p>
448 <p>Then, in your SSI-enabled document, you might do the
451 <!--#if expr="${Mac} && ${InternetExplorer}" --><br />
452 Apologetic text goes here<br />
453 <!--#else --><br />
454 Cool JavaScript code goes here<br />
458 <p>Not that I have anything against IE on Macs - I just
459 struggled for a few hours last week trying to get some
460 JavaScript working on IE on a Mac, when it was working
461 everywhere else. The above was the interim workaround.</p>
463 <p>Any other variable (either ones that you define, or normal
464 environment variables) can be used in conditional statements.
465 With Apache's ability to set environment variables with the
466 <code>SetEnvIf</code> directives, and other related directives,
467 this functionality can let you do some pretty involved dynamic
468 stuff without ever resorting to CGI.</p>
472 <section id="conclusion"><title>Conclusion</title>
474 <p>SSI is certainly not a replacement for CGI, or other
475 technologies used for generating dynamic web pages. But it is a
476 great way to add small amounts of dynamic content to pages,
477 without doing a lot of extra work.</p>