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 httpd 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>For example, you might place a directive into an existing HTML
73 <!--#echo var="DATE_LOCAL" -->
76 <p>And, when the page is served, this fragment will be evaluated and replaced with its value:</p>
79 Tuesday, 15-Jan-2013 19:28:54 EST
82 <p>The decision of when to use SSI, and when to have your page
83 entirely generated by some program, is usually a matter of how
84 much of the page is static, and how much needs to be
85 recalculated every time the page is served. SSI is a great way
86 to add small pieces of information, such as the current time - shown
87 above. But if a majority of your page is being generated at the time
88 that it is served, you need to look for some other solution.</p>
91 <section id="configuring">
92 <title>Configuring your server to permit SSI</title>
94 <p>To permit SSI on your server, you must have the following
95 directive either in your <code>httpd.conf</code> file, or in a
96 <code>.htaccess</code> file:</p>
97 <highlight language="config">
101 <p>This tells Apache that you want to permit files to be parsed
102 for SSI directives. Note that most configurations contain
103 multiple <directive module="core">Options</directive> directives
104 that can override each other. You will probably need to apply the
105 <code>Options</code> to the specific directory where you want SSI
106 enabled in order to assure that it gets evaluated last.</p>
108 <p>Not just any file is parsed for SSI directives. You have to
109 tell Apache which files should be parsed. There are two ways to
110 do this. You can tell Apache to parse any file with a
111 particular file extension, such as <code>.shtml</code>, with
112 the following directives:</p>
113 <highlight language="config">
114 AddType text/html .shtml
115 AddOutputFilter INCLUDES .shtml
118 <p>One disadvantage to this approach is that if you wanted to
119 add SSI directives to an existing page, you would have to
120 change the name of that page, and all links to that page, in
121 order to give it a <code>.shtml</code> extension, so that those
122 directives would be executed.</p>
124 <p>The other method is to use the <directive
125 module="mod_include">XBitHack</directive> directive:</p>
126 <highlight language="config">
130 <p><directive module="mod_include">XBitHack</directive>
131 tells Apache to parse files for SSI
132 directives if they have the execute bit set. So, to add SSI
133 directives to an existing page, rather than having to change
134 the file name, you would just need to make the file executable
135 using <code>chmod</code>.</p>
137 chmod +x pagename.html
140 <p>A brief comment about what not to do. You'll occasionally
141 see people recommending that you just tell Apache to parse all
142 <code>.html</code> files for SSI, so that you don't have to
143 mess with <code>.shtml</code> file names. These folks have
144 perhaps not heard about <directive
145 module="mod_include">XBitHack</directive>. The thing to
146 keep in mind is that, by doing this, you're requiring that
147 Apache read through every single file that it sends out to
148 clients, even if they don't contain any SSI directives. This
149 can slow things down quite a bit, and is not a good idea.</p>
151 <p>Of course, on Windows, there is no such thing as an execute
152 bit to set, so that limits your options a little.</p>
154 <p>In its default configuration, Apache does not send the last
155 modified date or content length HTTP headers on SSI pages,
156 because these values are difficult to calculate for dynamic
157 content. This can prevent your document from being cached, and
158 result in slower perceived client performance. There are two
159 ways to solve this:</p>
162 <li>Use the <code>XBitHack Full</code> configuration. This
163 tells Apache to determine the last modified date by looking
164 only at the date of the originally requested file, ignoring
165 the modification date of any included files.</li>
167 <li>Use the directives provided by
168 <module>mod_expires</module> to set an explicit expiration
169 time on your files, thereby letting browsers and proxies
170 know that it is acceptable to cache them.</li>
174 <section id="basic"><title>Basic SSI directives</title>
176 <p>SSI directives have the following syntax:</p>
178 <!--#function attribute=value attribute=value ... -->
181 <p>It is formatted like an HTML comment, so if you don't have
182 SSI correctly enabled, the browser will ignore it, but it will
183 still be visible in the HTML source. If you have SSI correctly
184 configured, the directive will be replaced with its
187 <p>The function can be one of a number of things, and we'll talk
188 some more about most of these in the next installment of this
189 series. For now, here are some examples of what you can do with
192 <section id="todaysdate"><title>Today's date</title>
195 <!--#echo var="DATE_LOCAL" -->
198 <p>The <code>echo</code> function just spits out the value of a
199 variable. There are a number of standard variables, which
200 include the whole set of environment variables that are
201 available to CGI programs. Also, you can define your own
202 variables with the <code>set</code> function.</p>
204 <p>If you don't like the format in which the date gets printed,
205 you can use the <code>config</code> function, with a
206 <code>timefmt</code> attribute, to modify that formatting.</p>
209 <!--#config timefmt="%A %B %d, %Y" --><br />
210 Today is <!--#echo var="DATE_LOCAL" -->
214 <section id="lastmodified"><title>Modification date of the file</title>
217 This document last modified <!--#flastmod file="index.html" -->
220 <p>This function is also subject to <code>timefmt</code> format
224 <section id="cgi"><title>Including the results of a CGI program</title>
226 <p>This is one of the more common uses of SSI - to output the
227 results of a CGI program, such as everybody's favorite, a ``hit
231 <!--#include virtual="/cgi-bin/counter.pl" -->
237 <section id="additionalexamples">
238 <title>Additional examples</title>
240 <p>Following are some specific examples of things you can do in
241 your HTML documents with SSI.</p>
243 <section id="docmodified"><title>When was this document
246 <p>Earlier, we mentioned that you could use SSI to inform the
247 user when the document was most recently modified. However, the
248 actual method for doing that was left somewhat in question. The
249 following code, placed in your HTML document, will put such a
250 time stamp on your page. Of course, you will have to have SSI
251 correctly enabled, as discussed above.</p>
253 <!--#config timefmt="%A %B %d, %Y" --><br />
254 This file last modified <!--#flastmod file="ssi.shtml" -->
257 <p>Of course, you will need to replace the
258 <code>ssi.shtml</code> with the actual name of the file that
259 you're referring to. This can be inconvenient if you're just
260 looking for a generic piece of code that you can paste into any
261 file, so you probably want to use the
262 <code>LAST_MODIFIED</code> variable instead:</p>
264 <!--#config timefmt="%D" --><br />
265 This file last modified <!--#echo var="LAST_MODIFIED" -->
268 <p>For more details on the <code>timefmt</code> format, go to
269 your favorite search site and look for <code>strftime</code>. The
270 syntax is the same.</p>
273 <section id="standard-footer">
274 <title>Including a standard footer</title>
276 <p>If you are managing any site that is more than a few pages,
277 you may find that making changes to all those pages can be a
278 real pain, particularly if you are trying to maintain some kind
279 of standard look across all those pages.</p>
281 <p>Using an include file for a header and/or a footer can
282 reduce the burden of these updates. You just have to make one
283 footer file, and then include it into each page with the
284 <code>include</code> SSI command. The <code>include</code>
285 function can determine what file to include with either the
286 <code>file</code> attribute, or the <code>virtual</code>
287 attribute. The <code>file</code> attribute is a file path,
288 <em>relative to the current directory</em>. That means that it
289 cannot be an absolute file path (starting with /), nor can it
290 contain ../ as part of that path. The <code>virtual</code>
291 attribute is probably more useful, and should specify a URL
292 relative to the document being served. It can start with a /,
293 but must be on the same server as the file being served.</p>
295 <!--#include virtual="/footer.html" -->
298 <p>I'll frequently combine the last two things, putting a
299 <code>LAST_MODIFIED</code> directive inside a footer file to be
300 included. SSI directives can be contained in the included file,
301 and includes can be nested - that is, the included file can
302 include another file, and so on.</p>
307 <section id="config">
308 <title>What else can I config?</title>
310 <p>In addition to being able to <code>config</code> the time
311 format, you can also <code>config</code> two other things.</p>
313 <p>Usually, when something goes wrong with your SSI directive,
314 you get the message</p>
316 [an error occurred while processing this directive]
319 <p>If you want to change that message to something else, you
320 can do so with the <code>errmsg</code> attribute to the
321 <code>config</code> function:</p>
323 <!--#config errmsg="[It appears that you don't know how to use SSI]" -->
326 <p>Hopefully, end users will never see this message, because
327 you will have resolved all the problems with your SSI
328 directives before your site goes live. (Right?)</p>
330 <p>And you can <code>config</code> the format in which file
331 sizes are returned with the <code>sizefmt</code> attribute. You
332 can specify <code>bytes</code> for a full count in bytes, or
333 <code>abbrev</code> for an abbreviated number in Kb or Mb, as
338 <title>Executing commands</title>
340 <p>I expect that I'll have an article some time in the coming
341 months about using SSI with small CGI programs. For now, here's
342 something else that you can do with the <code>exec</code>
343 function. You can actually have SSI execute a command using the
344 shell (<code>/bin/sh</code>, to be precise - or the DOS shell,
345 if you're on Win32). The following, for example, will give you
346 a directory listing.</p>
349 <!--#exec cmd="ls" --><br />
353 <p>or, on Windows</p>
356 <!--#exec cmd="dir" --><br />
360 <p>You might notice some strange formatting with this directive
361 on Windows, because the output from <code>dir</code> contains
362 the string ``<<code>dir</code>>'' in it, which confuses
365 <p>Note that this feature is exceedingly dangerous, as it will
366 execute whatever code happens to be embedded in the
367 <code>exec</code> tag. If you have any situation where users
368 can edit content on your web pages, such as with a
369 ``guestbook'', for example, make sure that you have this
370 feature disabled. You can allow SSI, but not the
371 <code>exec</code> feature, with the <code>IncludesNOEXEC</code>
372 argument to the <code>Options</code> directive.</p>
375 <section id="advanced">
376 <title>Advanced SSI techniques</title>
378 <p>In addition to spitting out content, Apache SSI gives you
379 the option of setting variables, and using those variables in
380 comparisons and conditionals.</p>
382 <section id="variables"><title>Setting variables</title>
384 <p>Using the <code>set</code> directive, you can set variables
385 for later use. We'll need this later in the discussion, so
386 we'll talk about it here. The syntax of this is as follows:</p>
388 <!--#set var="name" value="Rich" -->
391 <p>In addition to merely setting values literally like that, you
392 can use any other variable, including <a
393 href="../env.html">environment variables</a> or the variables
394 discussed above (like <code>LAST_MODIFIED</code>, for example) to
395 give values to your variables. You will specify that something is
396 a variable, rather than a literal string, by using the dollar sign
397 ($) before the name of the variable.</p>
399 <example> <!--#set var="modified" value="$LAST_MODIFIED" -->
402 <p>To put a literal dollar sign into the value of your
403 variable, you need to escape the dollar sign with a
406 <!--#set var="cost" value="\$100" -->
409 <p>Finally, if you want to put a variable in the midst of a
410 longer string, and there's a chance that the name of the
411 variable will run up against some other characters, and thus be
412 confused with those characters, you can place the name of the
413 variable in braces, to remove this confusion. (It's hard to
414 come up with a really good example of this, but hopefully
415 you'll get the point.)</p>
417 <!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" -->
421 <section id="conditional">
422 <title>Conditional expressions</title>
424 <p>Now that we have variables, and are able to set and compare
425 their values, we can use them to express conditionals. This
426 lets SSI be a tiny programming language of sorts.
427 <module>mod_include</module> provides an <code>if</code>,
428 <code>elif</code>, <code>else</code>, <code>endif</code>
429 structure for building conditional statements. This allows you
430 to effectively generate multiple logical pages out of one
433 <p>The structure of this conditional construct is:</p>
435 <!--#if expr="test_condition" --><br />
436 <!--#elif expr="test_condition" --><br />
437 <!--#else --><br />
441 <p>A <em>test_condition</em> can be any sort of logical
442 comparison - either comparing values to one another, or testing
443 the ``truth'' of a particular value. (A given string is true if
444 it is nonempty.) For a full list of the comparison operators
445 available to you, see the <module>mod_include</module>
448 <p>For example, if you wish to customize the text on your web page
449 based on the time of day, you could use the following recipe, placed
450 in the HTML page:</p>
454 <!--#if expr="%{TIME_HOUR} <12" --><br />
456 <!--#else --><br />
458 <!--#endif --><br />
461 <p>Any other variable (either ones that you define, or normal
462 environment variables) can be used in conditional statements.
463 See <a href="../expr.html">Expressions in Apache HTTP Server</a> for
464 more information on the expression evaluation engine.</p>
466 <p>With Apache's ability to set environment variables with the
467 <code>SetEnvIf</code> directives, and other related directives,
468 this functionality can let you do a wide variety of dynamic content
469 on the server side without resorting a full web application.</p>
473 <section id="conclusion"><title>Conclusion</title>
475 <p>SSI is certainly not a replacement for CGI, or other
476 technologies used for generating dynamic web pages. But it is a
477 great way to add small amounts of dynamic content to pages,
478 without doing a lot of extra work.</p>