1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 This file is generated from xml source: DO NOT EDIT
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 <title>Apache Tutorial: Introduction to Server Side Includes - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.0</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.0</a> > <a href="./">How-To / Tutorials</a></div><div id="page-content"><div id="preamble"><h1>Apache Tutorial: Introduction to Server Side Includes</h1>
21 <p><span>Available Languages: </span><a href="../en/howto/ssi.html" title="English"> en </a> |
22 <a href="../ja/howto/ssi.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
23 <a href="../ko/howto/ssi.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
26 <p>Server-side includes provide a means to add dynamic content to
27 existing HTML documents.</p>
29 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#related">Introduction</a></li>
30 <li><img alt="" src="../images/down.gif" /> <a href="#what">What are SSI?</a></li>
31 <li><img alt="" src="../images/down.gif" /> <a href="#configuring">Configuring your server to permit SSI</a></li>
32 <li><img alt="" src="../images/down.gif" /> <a href="#basic">Basic SSI directives</a></li>
33 <li><img alt="" src="../images/down.gif" /> <a href="#additionalexamples">Additional examples</a></li>
34 <li><img alt="" src="../images/down.gif" /> <a href="#config">What else can I config?</a></li>
35 <li><img alt="" src="../images/down.gif" /> <a href="#exec">Executing commands</a></li>
36 <li><img alt="" src="../images/down.gif" /> <a href="#advanced">Advanced SSI techniques</a></li>
37 <li><img alt="" src="../images/down.gif" /> <a href="#conclusion">Conclusion</a></li>
39 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
41 <h2><a name="related" id="related">Introduction</a></h2>
42 <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="../mod/mod_include.html">mod_include</a></code></li><li><code class="module"><a href="../mod/mod_cgi.html">mod_cgi</a></code></li><li><code class="module"><a href="../mod/mod_expires.html">mod_expires</a></code></li></ul></td><td><ul><li><code class="directive"><a href="../mod/core.html#options">Options</a></code></li><li><code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code></li><li><code class="directive"><a href="../mod/mod_mime.html#addtype">AddType</a></code></li><li><code class="directive"><a href="../mod/core.html#setoutputfilter">SetOutputFilter</a></code></li><li><code class="directive"><a href="../mod/mod_setenvif.html#browsermatchnocase">BrowserMatchNoCase</a></code></li></ul></td></tr></table>
44 <p>This article deals with Server Side Includes, usually called
45 simply SSI. In this article, I'll talk about configuring your
46 server to permit SSI, and introduce some basic SSI techniques
47 for adding dynamic content to your existing HTML pages.</p>
49 <p>In the latter part of the article, we'll talk about some of
50 the somewhat more advanced things that can be done with SSI,
51 such as conditional statements in your SSI directives.</p>
53 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
55 <h2><a name="what" id="what">What are SSI?</a></h2>
57 <p>SSI (Server Side Includes) are directives that are placed in
58 HTML pages, and evaluated on the server while the pages are
59 being served. They let you add dynamically generated content to
60 an existing HTML page, without having to serve the entire page
61 via a CGI program, or other dynamic technology.</p>
63 <p>The decision of when to use SSI, and when to have your page
64 entirely generated by some program, is usually a matter of how
65 much of the page is static, and how much needs to be
66 recalculated every time the page is served. SSI is a great way
67 to add small pieces of information, such as the current time.
68 But if a majority of your page is being generated at the time
69 that it is served, you need to look for some other
71 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
73 <h2><a name="configuring" id="configuring">Configuring your server to permit SSI</a></h2>
76 <p>To permit SSI on your server, you must have the following
77 directive either in your <code>httpd.conf</code> file, or in a
78 <code>.htaccess</code> file:</p>
79 <div class="example"><p><code>
83 <p>This tells Apache that you want to permit files to be parsed
84 for SSI directives. Note that most configurations contain
85 multiple <code class="directive"><a href="../mod/core.html#options">Options</a></code> directives
86 that can override each other. You will probably need to apply the
87 <code>Options</code> to the specific directory where you want SSI
88 enabled in order to assure that it gets evaluated last.</p>
90 <p>Not just any file is parsed for SSI directives. You have to
91 tell Apache which files should be parsed. There are two ways to
92 do this. You can tell Apache to parse any file with a
93 particular file extension, such as <code>.shtml</code>, with
94 the following directives:</p>
95 <div class="example"><p><code>
96 AddType text/html .shtml<br />
97 AddOutputFilter INCLUDES .shtml
100 <p>One disadvantage to this approach is that if you wanted to
101 add SSI directives to an existing page, you would have to
102 change the name of that page, and all links to that page, in
103 order to give it a <code>.shtml</code> extension, so that those
104 directives would be executed.</p>
106 <p>The other method is to use the <code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code> directive:</p>
107 <div class="example"><p><code>
111 <p><code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code>
112 tells Apache to parse files for SSI
113 directives if they have the execute bit set. So, to add SSI
114 directives to an existing page, rather than having to change
115 the file name, you would just need to make the file executable
116 using <code>chmod</code>.</p>
117 <div class="example"><p><code>
118 chmod +x pagename.html
121 <p>A brief comment about what not to do. You'll occasionally
122 see people recommending that you just tell Apache to parse all
123 <code>.html</code> files for SSI, so that you don't have to
124 mess with <code>.shtml</code> file names. These folks have
125 perhaps not heard about <code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code>. The thing to
126 keep in mind is that, by doing this, you're requiring that
127 Apache read through every single file that it sends out to
128 clients, even if they don't contain any SSI directives. This
129 can slow things down quite a bit, and is not a good idea.</p>
131 <p>Of course, on Windows, there is no such thing as an execute
132 bit to set, so that limits your options a little.</p>
134 <p>In its default configuration, Apache does not send the last
135 modified date or content length HTTP headers on SSI pages,
136 because these values are difficult to calculate for dynamic
137 content. This can prevent your document from being cached, and
138 result in slower perceived client performance. There are two
139 ways to solve this:</p>
142 <li>Use the <code>XBitHack Full</code> configuration. This
143 tells Apache to determine the last modified date by looking
144 only at the date of the originally requested file, ignoring
145 the modification date of any included files.</li>
147 <li>Use the directives provided by
148 <code class="module"><a href="../mod/mod_expires.html">mod_expires</a></code> to set an explicit expiration
149 time on your files, thereby letting browsers and proxies
150 know that it is acceptable to cache them.</li>
152 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
153 <div class="section">
154 <h2><a name="basic" id="basic">Basic SSI directives</a></h2>
156 <p>SSI directives have the following syntax:</p>
157 <div class="example"><p><code>
158 <!--#element attribute=value attribute=value ... -->
161 <p>It is formatted like an HTML comment, so if you don't have
162 SSI correctly enabled, the browser will ignore it, but it will
163 still be visible in the HTML source. If you have SSI correctly
164 configured, the directive will be replaced with its
167 <p>The element can be one of a number of things, and we'll talk
168 some more about most of these in the next installment of this
169 series. For now, here are some examples of what you can do with
172 <h3><a name="todaysdate" id="todaysdate">Today's date</a></h3>
174 <div class="example"><p><code>
175 <!--#echo var="DATE_LOCAL" -->
178 <p>The <code>echo</code> element just spits out the value of a
179 variable. There are a number of standard variables, which
180 include the whole set of environment variables that are
181 available to CGI programs. Also, you can define your own
182 variables with the <code>set</code> element.</p>
184 <p>If you don't like the format in which the date gets printed,
185 you can use the <code>config</code> element, with a
186 <code>timefmt</code> attribute, to modify that formatting.</p>
188 <div class="example"><p><code>
189 <!--#config timefmt="%A %B %d, %Y" --><br />
190 Today is <!--#echo var="DATE_LOCAL" -->
194 <h3><a name="lastmodified" id="lastmodified">Modification date of the file</a></h3>
196 <div class="example"><p><code>
197 This document last modified <!--#flastmod file="index.html" -->
200 <p>This element is also subject to <code>timefmt</code> format
204 <h3><a name="cgi" id="cgi">Including the results of a CGI program</a></h3>
206 <p>This is one of the more common uses of SSI - to output the
207 results of a CGI program, such as everybody's favorite, a ``hit
210 <div class="example"><p><code>
211 <!--#include virtual="/cgi-bin/counter.pl" -->
215 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
216 <div class="section">
217 <h2><a name="additionalexamples" id="additionalexamples">Additional examples</a></h2>
220 <p>Following are some specific examples of things you can do in
221 your HTML documents with SSI.</p>
223 <h3><a name="docmodified" id="docmodified">When was this document
226 <p>Earlier, we mentioned that you could use SSI to inform the
227 user when the document was most recently modified. However, the
228 actual method for doing that was left somewhat in question. The
229 following code, placed in your HTML document, will put such a
230 time stamp on your page. Of course, you will have to have SSI
231 correctly enabled, as discussed above.</p>
232 <div class="example"><p><code>
233 <!--#config timefmt="%A %B %d, %Y" --><br />
234 This file last modified <!--#flastmod file="ssi.shtml" -->
237 <p>Of course, you will need to replace the
238 <code>ssi.shtml</code> with the actual name of the file that
239 you're referring to. This can be inconvenient if you're just
240 looking for a generic piece of code that you can paste into any
241 file, so you probably want to use the
242 <code>LAST_MODIFIED</code> variable instead:</p>
243 <div class="example"><p><code>
244 <!--#config timefmt="%D" --><br />
245 This file last modified <!--#echo var="LAST_MODIFIED" -->
248 <p>For more details on the <code>timefmt</code> format, go to
249 your favorite search site and look for <code>strftime</code>. The
250 syntax is the same.</p>
253 <h3><a name="standard-footer" id="standard-footer">Including a standard footer</a></h3>
256 <p>If you are managing any site that is more than a few pages,
257 you may find that making changes to all those pages can be a
258 real pain, particularly if you are trying to maintain some kind
259 of standard look across all those pages.</p>
261 <p>Using an include file for a header and/or a footer can
262 reduce the burden of these updates. You just have to make one
263 footer file, and then include it into each page with the
264 <code>include</code> SSI command. The <code>include</code>
265 element can determine what file to include with either the
266 <code>file</code> attribute, or the <code>virtual</code>
267 attribute. The <code>file</code> attribute is a file path,
268 <em>relative to the current directory</em>. That means that it
269 cannot be an absolute file path (starting with /), nor can it
270 contain ../ as part of that path. The <code>virtual</code>
271 attribute is probably more useful, and should specify a URL
272 relative to the document being served. It can start with a /,
273 but must be on the same server as the file being served.</p>
274 <div class="example"><p><code>
275 <!--#include virtual="/footer.html" -->
278 <p>I'll frequently combine the last two things, putting a
279 <code>LAST_MODIFIED</code> directive inside a footer file to be
280 included. SSI directives can be contained in the included file,
281 and includes can be nested - that is, the included file can
282 include another file, and so on.</p>
285 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
286 <div class="section">
287 <h2><a name="config" id="config">What else can I config?</a></h2>
290 <p>In addition to being able to <code>config</code> the time
291 format, you can also <code>config</code> two other things.</p>
293 <p>Usually, when something goes wrong with your SSI directive,
294 you get the message</p>
295 <div class="example"><p><code>
296 [an error occurred while processing this directive]
299 <p>If you want to change that message to something else, you
300 can do so with the <code>errmsg</code> attribute to the
301 <code>config</code> element:</p>
302 <div class="example"><p><code>
303 <!--#config errmsg="[It appears that you don't know how to use SSI]" -->
306 <p>Hopefully, end users will never see this message, because
307 you will have resolved all the problems with your SSI
308 directives before your site goes live. (Right?)</p>
310 <p>And you can <code>config</code> the format in which file
311 sizes are returned with the <code>sizefmt</code> attribute. You
312 can specify <code>bytes</code> for a full count in bytes, or
313 <code>abbrev</code> for an abbreviated number in Kb or Mb, as
315 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
316 <div class="section">
317 <h2><a name="exec" id="exec">Executing commands</a></h2>
320 <p>I expect that I'll have an article some time in the coming
321 months about using SSI with small CGI programs. For now, here's
322 something else that you can do with the <code>exec</code>
323 element. You can actually have SSI execute a command using the
324 shell (<code>/bin/sh</code>, to be precise - or the DOS shell,
325 if you're on Win32). The following, for example, will give you
326 a directory listing.</p>
327 <div class="example"><p><code>
329 <!--#exec cmd="ls" --><br />
333 <p>or, on Windows</p>
334 <div class="example"><p><code>
336 <!--#exec cmd="dir" --><br />
340 <p>You might notice some strange formatting with this directive
341 on Windows, because the output from <code>dir</code> contains
342 the string ``<<code>dir</code>>'' in it, which confuses
345 <p>Note that this feature is exceedingly dangerous, as it will
346 execute whatever code happens to be embedded in the
347 <code>exec</code> tag. If you have any situation where users
348 can edit content on your web pages, such as with a
349 ``guestbook'', for example, make sure that you have this
350 feature disabled. You can allow SSI, but not the
351 <code>exec</code> feature, with the <code>IncludesNOEXEC</code>
352 argument to the <code>Options</code> directive.</p>
353 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
354 <div class="section">
355 <h2><a name="advanced" id="advanced">Advanced SSI techniques</a></h2>
358 <p>In addition to spitting out content, Apache SSI gives you
359 the option of setting variables, and using those variables in
360 comparisons and conditionals.</p>
362 <h3><a name="caveat" id="caveat">Caveat</a></h3>
364 <p>Most of the features discussed in this article are only
365 available to you if you are running Apache 1.2 or later. Of
366 course, if you are not running Apache 1.2 or later, you need to
367 upgrade immediately, if not sooner. Go on. Do it now. We'll
371 <h3><a name="variables" id="variables">Setting variables</a></h3>
373 <p>Using the <code>set</code> directive, you can set variables
374 for later use. We'll need this later in the discussion, so
375 we'll talk about it here. The syntax of this is as follows:</p>
376 <div class="example"><p><code>
377 <!--#set var="name" value="Rich" -->
380 <p>In addition to merely setting values literally like that, you
381 can use any other variable, including <a 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 <div class="example"><p><code> <!--#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
393 <div class="example"><p><code>
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>
404 <div class="example"><p><code>
405 <!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" -->
409 <h3><a name="conditional" id="conditional">Conditional expressions</a></h3>
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 <code class="module"><a href="../mod/mod_include.html">mod_include</a></code> 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>
422 <div class="example"><p><code>
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 <code class="module"><a href="../mod/mod_include.html">mod_include</a></code>
434 documentation. Here are some examples of how one might use this
437 <p>In your configuration file, you could put the following
439 <div class="example"><p><code>
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
450 <div class="example"><p><code>
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>
470 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
471 <div class="section">
472 <h2><a name="conclusion" id="conclusion">Conclusion</a></h2>
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>
479 <div class="bottomlang">
480 <p><span>Available Languages: </span><a href="../en/howto/ssi.html" title="English"> en </a> |
481 <a href="../ja/howto/ssi.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
482 <a href="../ko/howto/ssi.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
483 </div><div id="footer">
484 <p class="apache">Copyright 2009 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
485 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>