switch to a 60 bit hash
[httpd-crcsyncproxy.git] / docs / manual / howto / ssi.html.en
blobb2e8078b28430272ae10892975a0a71412018e5f
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
7 -->
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.3</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
18 <div id="path">
19 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.3</a> &gt; <a href="./">How-To / Tutorials</a></div><div id="page-content"><div id="preamble"><h1>Apache Tutorial: Introduction to Server Side Includes</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="../en/howto/ssi.html" title="English">&nbsp;en&nbsp;</a> |
22 <a href="../fr/howto/ssi.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
23 <a href="../ja/howto/ssi.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
24 <a href="../ko/howto/ssi.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
25 </div>
27 <p>Server-side includes provide a means to add dynamic content to
28 existing HTML documents.</p>
29 </div>
30 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#related">Introduction</a></li>
31 <li><img alt="" src="../images/down.gif" /> <a href="#what">What are SSI?</a></li>
32 <li><img alt="" src="../images/down.gif" /> <a href="#configuring">Configuring your server to permit SSI</a></li>
33 <li><img alt="" src="../images/down.gif" /> <a href="#basic">Basic SSI directives</a></li>
34 <li><img alt="" src="../images/down.gif" /> <a href="#additionalexamples">Additional examples</a></li>
35 <li><img alt="" src="../images/down.gif" /> <a href="#config">What else can I config?</a></li>
36 <li><img alt="" src="../images/down.gif" /> <a href="#exec">Executing commands</a></li>
37 <li><img alt="" src="../images/down.gif" /> <a href="#advanced">Advanced SSI techniques</a></li>
38 <li><img alt="" src="../images/down.gif" /> <a href="#conclusion">Conclusion</a></li>
39 </ul></div>
40 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
41 <div class="section">
42 <h2><a name="related" id="related">Introduction</a></h2>
43 <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>
45 <p>This article deals with Server Side Includes, usually called
46 simply SSI. In this article, I'll talk about configuring your
47 server to permit SSI, and introduce some basic SSI techniques
48 for adding dynamic content to your existing HTML pages.</p>
50 <p>In the latter part of the article, we'll talk about some of
51 the somewhat more advanced things that can be done with SSI,
52 such as conditional statements in your SSI directives.</p>
54 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
55 <div class="section">
56 <h2><a name="what" id="what">What are SSI?</a></h2>
58 <p>SSI (Server Side Includes) are directives that are placed in
59 HTML pages, and evaluated on the server while the pages are
60 being served. They let you add dynamically generated content to
61 an existing HTML page, without having to serve the entire page
62 via a CGI program, or other dynamic technology.</p>
64 <p>The decision of when to use SSI, and when to have your page
65 entirely generated by some program, is usually a matter of how
66 much of the page is static, and how much needs to be
67 recalculated every time the page is served. SSI is a great way
68 to add small pieces of information, such as the current time.
69 But if a majority of your page is being generated at the time
70 that it is served, you need to look for some other
71 solution.</p>
72 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
73 <div class="section">
74 <h2><a name="configuring" id="configuring">Configuring your server to permit SSI</a></h2>
77 <p>To permit SSI on your server, you must have the following
78 directive either in your <code>httpd.conf</code> file, or in a
79 <code>.htaccess</code> file:</p>
80 <div class="example"><p><code>
81 Options +Includes
82 </code></p></div>
84 <p>This tells Apache that you want to permit files to be parsed
85 for SSI directives. Note that most configurations contain
86 multiple <code class="directive"><a href="../mod/core.html#options">Options</a></code> directives
87 that can override each other. You will probably need to apply the
88 <code>Options</code> to the specific directory where you want SSI
89 enabled in order to assure that it gets evaluated last.</p>
91 <p>Not just any file is parsed for SSI directives. You have to
92 tell Apache which files should be parsed. There are two ways to
93 do this. You can tell Apache to parse any file with a
94 particular file extension, such as <code>.shtml</code>, with
95 the following directives:</p>
96 <div class="example"><p><code>
97 AddType text/html .shtml<br />
98 AddOutputFilter INCLUDES .shtml
99 </code></p></div>
101 <p>One disadvantage to this approach is that if you wanted to
102 add SSI directives to an existing page, you would have to
103 change the name of that page, and all links to that page, in
104 order to give it a <code>.shtml</code> extension, so that those
105 directives would be executed.</p>
107 <p>The other method is to use the <code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code> directive:</p>
108 <div class="example"><p><code>
109 XBitHack on
110 </code></p></div>
112 <p><code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code>
113 tells Apache to parse files for SSI
114 directives if they have the execute bit set. So, to add SSI
115 directives to an existing page, rather than having to change
116 the file name, you would just need to make the file executable
117 using <code>chmod</code>.</p>
118 <div class="example"><p><code>
119 chmod +x pagename.html
120 </code></p></div>
122 <p>A brief comment about what not to do. You'll occasionally
123 see people recommending that you just tell Apache to parse all
124 <code>.html</code> files for SSI, so that you don't have to
125 mess with <code>.shtml</code> file names. These folks have
126 perhaps not heard about <code class="directive"><a href="../mod/mod_include.html#xbithack">XBitHack</a></code>. The thing to
127 keep in mind is that, by doing this, you're requiring that
128 Apache read through every single file that it sends out to
129 clients, even if they don't contain any SSI directives. This
130 can slow things down quite a bit, and is not a good idea.</p>
132 <p>Of course, on Windows, there is no such thing as an execute
133 bit to set, so that limits your options a little.</p>
135 <p>In its default configuration, Apache does not send the last
136 modified date or content length HTTP headers on SSI pages,
137 because these values are difficult to calculate for dynamic
138 content. This can prevent your document from being cached, and
139 result in slower perceived client performance. There are two
140 ways to solve this:</p>
142 <ol>
143 <li>Use the <code>XBitHack Full</code> configuration. This
144 tells Apache to determine the last modified date by looking
145 only at the date of the originally requested file, ignoring
146 the modification date of any included files.</li>
148 <li>Use the directives provided by
149 <code class="module"><a href="../mod/mod_expires.html">mod_expires</a></code> to set an explicit expiration
150 time on your files, thereby letting browsers and proxies
151 know that it is acceptable to cache them.</li>
152 </ol>
153 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
154 <div class="section">
155 <h2><a name="basic" id="basic">Basic SSI directives</a></h2>
157 <p>SSI directives have the following syntax:</p>
158 <div class="example"><p><code>
159 &lt;!--#element attribute=value attribute=value ... --&gt;
160 </code></p></div>
162 <p>It is formatted like an HTML comment, so if you don't have
163 SSI correctly enabled, the browser will ignore it, but it will
164 still be visible in the HTML source. If you have SSI correctly
165 configured, the directive will be replaced with its
166 results.</p>
168 <p>The element can be one of a number of things, and we'll talk
169 some more about most of these in the next installment of this
170 series. For now, here are some examples of what you can do with
171 SSI</p>
173 <h3><a name="todaysdate" id="todaysdate">Today's date</a></h3>
175 <div class="example"><p><code>
176 &lt;!--#echo var="DATE_LOCAL" --&gt;
177 </code></p></div>
179 <p>The <code>echo</code> element just spits out the value of a
180 variable. There are a number of standard variables, which
181 include the whole set of environment variables that are
182 available to CGI programs. Also, you can define your own
183 variables with the <code>set</code> element.</p>
185 <p>If you don't like the format in which the date gets printed,
186 you can use the <code>config</code> element, with a
187 <code>timefmt</code> attribute, to modify that formatting.</p>
189 <div class="example"><p><code>
190 &lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
191 Today is &lt;!--#echo var="DATE_LOCAL" --&gt;
192 </code></p></div>
195 <h3><a name="lastmodified" id="lastmodified">Modification date of the file</a></h3>
197 <div class="example"><p><code>
198 This document last modified &lt;!--#flastmod file="index.html" --&gt;
199 </code></p></div>
201 <p>This element is also subject to <code>timefmt</code> format
202 configurations.</p>
205 <h3><a name="cgi" id="cgi">Including the results of a CGI program</a></h3>
207 <p>This is one of the more common uses of SSI - to output the
208 results of a CGI program, such as everybody's favorite, a ``hit
209 counter.''</p>
211 <div class="example"><p><code>
212 &lt;!--#include virtual="/cgi-bin/counter.pl" --&gt;
213 </code></p></div>
216 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
217 <div class="section">
218 <h2><a name="additionalexamples" id="additionalexamples">Additional examples</a></h2>
221 <p>Following are some specific examples of things you can do in
222 your HTML documents with SSI.</p>
224 <h3><a name="docmodified" id="docmodified">When was this document
225 modified?</a></h3>
227 <p>Earlier, we mentioned that you could use SSI to inform the
228 user when the document was most recently modified. However, the
229 actual method for doing that was left somewhat in question. The
230 following code, placed in your HTML document, will put such a
231 time stamp on your page. Of course, you will have to have SSI
232 correctly enabled, as discussed above.</p>
233 <div class="example"><p><code>
234 &lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
235 This file last modified &lt;!--#flastmod file="ssi.shtml" --&gt;
236 </code></p></div>
238 <p>Of course, you will need to replace the
239 <code>ssi.shtml</code> with the actual name of the file that
240 you're referring to. This can be inconvenient if you're just
241 looking for a generic piece of code that you can paste into any
242 file, so you probably want to use the
243 <code>LAST_MODIFIED</code> variable instead:</p>
244 <div class="example"><p><code>
245 &lt;!--#config timefmt="%D" --&gt;<br />
246 This file last modified &lt;!--#echo var="LAST_MODIFIED" --&gt;
247 </code></p></div>
249 <p>For more details on the <code>timefmt</code> format, go to
250 your favorite search site and look for <code>strftime</code>. The
251 syntax is the same.</p>
254 <h3><a name="standard-footer" id="standard-footer">Including a standard footer</a></h3>
257 <p>If you are managing any site that is more than a few pages,
258 you may find that making changes to all those pages can be a
259 real pain, particularly if you are trying to maintain some kind
260 of standard look across all those pages.</p>
262 <p>Using an include file for a header and/or a footer can
263 reduce the burden of these updates. You just have to make one
264 footer file, and then include it into each page with the
265 <code>include</code> SSI command. The <code>include</code>
266 element can determine what file to include with either the
267 <code>file</code> attribute, or the <code>virtual</code>
268 attribute. The <code>file</code> attribute is a file path,
269 <em>relative to the current directory</em>. That means that it
270 cannot be an absolute file path (starting with /), nor can it
271 contain ../ as part of that path. The <code>virtual</code>
272 attribute is probably more useful, and should specify a URL
273 relative to the document being served. It can start with a /,
274 but must be on the same server as the file being served.</p>
275 <div class="example"><p><code>
276 &lt;!--#include virtual="/footer.html" --&gt;
277 </code></p></div>
279 <p>I'll frequently combine the last two things, putting a
280 <code>LAST_MODIFIED</code> directive inside a footer file to be
281 included. SSI directives can be contained in the included file,
282 and includes can be nested - that is, the included file can
283 include another file, and so on.</p>
286 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
287 <div class="section">
288 <h2><a name="config" id="config">What else can I config?</a></h2>
291 <p>In addition to being able to <code>config</code> the time
292 format, you can also <code>config</code> two other things.</p>
294 <p>Usually, when something goes wrong with your SSI directive,
295 you get the message</p>
296 <div class="example"><p><code>
297 [an error occurred while processing this directive]
298 </code></p></div>
300 <p>If you want to change that message to something else, you
301 can do so with the <code>errmsg</code> attribute to the
302 <code>config</code> element:</p>
303 <div class="example"><p><code>
304 &lt;!--#config errmsg="[It appears that you don't know how to use SSI]" --&gt;
305 </code></p></div>
307 <p>Hopefully, end users will never see this message, because
308 you will have resolved all the problems with your SSI
309 directives before your site goes live. (Right?)</p>
311 <p>And you can <code>config</code> the format in which file
312 sizes are returned with the <code>sizefmt</code> attribute. You
313 can specify <code>bytes</code> for a full count in bytes, or
314 <code>abbrev</code> for an abbreviated number in Kb or Mb, as
315 appropriate.</p>
316 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
317 <div class="section">
318 <h2><a name="exec" id="exec">Executing commands</a></h2>
321 <p>I expect that I'll have an article some time in the coming
322 months about using SSI with small CGI programs. For now, here's
323 something else that you can do with the <code>exec</code>
324 element. You can actually have SSI execute a command using the
325 shell (<code>/bin/sh</code>, to be precise - or the DOS shell,
326 if you're on Win32). The following, for example, will give you
327 a directory listing.</p>
328 <div class="example"><p><code>
329 &lt;pre&gt;<br />
330 &lt;!--#exec cmd="ls" --&gt;<br />
331 &lt;/pre&gt;
332 </code></p></div>
334 <p>or, on Windows</p>
335 <div class="example"><p><code>
336 &lt;pre&gt;<br />
337 &lt;!--#exec cmd="dir" --&gt;<br />
338 &lt;/pre&gt;
339 </code></p></div>
341 <p>You might notice some strange formatting with this directive
342 on Windows, because the output from <code>dir</code> contains
343 the string ``&lt;<code>dir</code>&gt;'' in it, which confuses
344 browsers.</p>
346 <p>Note that this feature is exceedingly dangerous, as it will
347 execute whatever code happens to be embedded in the
348 <code>exec</code> tag. If you have any situation where users
349 can edit content on your web pages, such as with a
350 ``guestbook'', for example, make sure that you have this
351 feature disabled. You can allow SSI, but not the
352 <code>exec</code> feature, with the <code>IncludesNOEXEC</code>
353 argument to the <code>Options</code> directive.</p>
354 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
355 <div class="section">
356 <h2><a name="advanced" id="advanced">Advanced SSI techniques</a></h2>
359 <p>In addition to spitting out content, Apache SSI gives you
360 the option of setting variables, and using those variables in
361 comparisons and conditionals.</p>
363 <h3><a name="caveat" id="caveat">Caveat</a></h3>
365 <p>Most of the features discussed in this article are only
366 available to you if you are running Apache 1.2 or later. Of
367 course, if you are not running Apache 1.2 or later, you need to
368 upgrade immediately, if not sooner. Go on. Do it now. We'll
369 wait.</p>
372 <h3><a name="variables" id="variables">Setting variables</a></h3>
374 <p>Using the <code>set</code> directive, you can set variables
375 for later use. We'll need this later in the discussion, so
376 we'll talk about it here. The syntax of this is as follows:</p>
377 <div class="example"><p><code>
378 &lt;!--#set var="name" value="Rich" --&gt;
379 </code></p></div>
381 <p>In addition to merely setting values literally like that, you
382 can use any other variable, including <a href="../env.html">environment variables</a> or the variables
383 discussed above (like <code>LAST_MODIFIED</code>, for example) to
384 give values to your variables. You will specify that something is
385 a variable, rather than a literal string, by using the dollar sign
386 ($) before the name of the variable.</p>
388 <div class="example"><p><code> &lt;!--#set var="modified" value="$LAST_MODIFIED" --&gt;
389 </code></p></div>
391 <p>To put a literal dollar sign into the value of your
392 variable, you need to escape the dollar sign with a
393 backslash.</p>
394 <div class="example"><p><code>
395 &lt;!--#set var="cost" value="\$100" --&gt;
396 </code></p></div>
398 <p>Finally, if you want to put a variable in the midst of a
399 longer string, and there's a chance that the name of the
400 variable will run up against some other characters, and thus be
401 confused with those characters, you can place the name of the
402 variable in braces, to remove this confusion. (It's hard to
403 come up with a really good example of this, but hopefully
404 you'll get the point.)</p>
405 <div class="example"><p><code>
406 &lt;!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --&gt;
407 </code></p></div>
410 <h3><a name="conditional" id="conditional">Conditional expressions</a></h3>
413 <p>Now that we have variables, and are able to set and compare
414 their values, we can use them to express conditionals. This
415 lets SSI be a tiny programming language of sorts.
416 <code class="module"><a href="../mod/mod_include.html">mod_include</a></code> provides an <code>if</code>,
417 <code>elif</code>, <code>else</code>, <code>endif</code>
418 structure for building conditional statements. This allows you
419 to effectively generate multiple logical pages out of one
420 actual page.</p>
422 <p>The structure of this conditional construct is:</p>
423 <div class="example"><p><code>
424 &lt;!--#if expr="test_condition" --&gt;<br />
425 &lt;!--#elif expr="test_condition" --&gt;<br />
426 &lt;!--#else --&gt;<br />
427 &lt;!--#endif --&gt;
428 </code></p></div>
430 <p>A <em>test_condition</em> can be any sort of logical
431 comparison - either comparing values to one another, or testing
432 the ``truth'' of a particular value. (A given string is true if
433 it is nonempty.) For a full list of the comparison operators
434 available to you, see the <code class="module"><a href="../mod/mod_include.html">mod_include</a></code>
435 documentation. Here are some examples of how one might use this
436 construct.</p>
438 <p>In your configuration file, you could put the following
439 line:</p>
440 <div class="example"><p><code>
441 BrowserMatchNoCase macintosh Mac<br />
442 BrowserMatchNoCase MSIE InternetExplorer
443 </code></p></div>
445 <p>This will set environment variables ``Mac'' and
446 ``InternetExplorer'' to true, if the client is running Internet
447 Explorer on a Macintosh.</p>
449 <p>Then, in your SSI-enabled document, you might do the
450 following:</p>
451 <div class="example"><p><code>
452 &lt;!--#if expr="${Mac} &amp;&amp; ${InternetExplorer}" --&gt;<br />
453 Apologetic text goes here<br />
454 &lt;!--#else --&gt;<br />
455 Cool JavaScript code goes here<br />
456 &lt;!--#endif --&gt;
457 </code></p></div>
459 <p>Not that I have anything against IE on Macs - I just
460 struggled for a few hours last week trying to get some
461 JavaScript working on IE on a Mac, when it was working
462 everywhere else. The above was the interim workaround.</p>
464 <p>Any other variable (either ones that you define, or normal
465 environment variables) can be used in conditional statements.
466 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 some pretty involved dynamic
469 stuff without ever resorting to CGI.</p>
471 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
472 <div class="section">
473 <h2><a name="conclusion" id="conclusion">Conclusion</a></h2>
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>
479 </div></div>
480 <div class="bottomlang">
481 <p><span>Available Languages: </span><a href="../en/howto/ssi.html" title="English">&nbsp;en&nbsp;</a> |
482 <a href="../fr/howto/ssi.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
483 <a href="../ja/howto/ssi.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
484 <a href="../ko/howto/ssi.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
485 </div><div id="footer">
486 <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>
487 <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>
488 </body></html>