]> granicus.if.org Git - apache/blob - docs/manual/howto/ssi.xml
b40dafc8fa918a3feb4642496f077b35b5ced0f7
[apache] / docs / manual / howto / ssi.xml
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$ -->
5
6 <!--
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <manualpage metafile="ssi.xml.meta">
24 <parentdocument href="./">How-To / Tutorials</parentdocument>
25
26 <title>Apache Tutorial: Introduction to Server Side Includes</title>
27
28 <summary>
29 <p>Server-side includes provide a means to add dynamic content to
30 existing HTML documents.</p>
31 </summary>
32
33 <section id="related"><title>Introduction</title>
34  <related>
35     <modulelist>
36     <module>mod_include</module>
37     <module>mod_cgi</module>
38     <module>mod_expires</module>
39     </modulelist>
40
41     <directivelist>
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>
47     </directivelist>
48 </related>
49
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>
54
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>
58
59 </section>
60
61 <section id="what"><title>What are SSI?</title>
62
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>
68
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
76     solution.</p>
77 </section>
78
79 <section id="configuring">
80 <title>Configuring your server to permit SSI</title>
81
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>
85 <example>
86         Options +Includes
87 </example>
88
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>
95
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>
101 <example>
102         AddType text/html .shtml<br />
103         AddOutputFilter INCLUDES .shtml
104 </example>
105
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>
111
112     <p>The other method is to use the <directive 
113     module="mod_include">XBitHack</directive> directive:</p>
114 <example>
115         XBitHack on
116 </example>
117
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>
124 <example>
125         chmod +x pagename.html
126 </example>
127
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>
138
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>
141
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>
148
149     <ol>
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>
154
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>
159     </ol>
160 </section>
161
162 <section id="basic"><title>Basic SSI directives</title>
163
164     <p>SSI directives have the following syntax:</p>
165 <example>
166         &lt;!--#element attribute=value attribute=value ... --&gt;
167 </example>
168
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
173     results.</p>
174
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
178     SSI</p>
179
180 <section id="todaysdate"><title>Today's date</title>
181
182 <example>
183         &lt;!--#echo var="DATE_LOCAL" --&gt;
184 </example>
185
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>
191
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>
195
196 <example>
197         &lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
198         Today is &lt;!--#echo var="DATE_LOCAL" --&gt;
199 </example>
200 </section>
201
202 <section id="lastmodified"><title>Modification date of the file</title>
203
204 <example>
205         This document last modified &lt;!--#flastmod file="index.html" --&gt;
206 </example>
207
208     <p>This element is also subject to <code>timefmt</code> format
209     configurations.</p>
210 </section>
211
212 <section id="cgi"><title>Including the results of a CGI program</title>
213
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
216     counter.''</p>
217
218 <example>
219         &lt;!--#include virtual="/cgi-bin/counter.pl" --&gt;
220 </example>
221
222 </section>
223 </section>
224
225 <section id="additionalexamples">
226 <title>Additional examples</title>
227
228     <p>Following are some specific examples of things you can do in
229     your HTML documents with SSI.</p>
230
231 <section id="docmodified"><title>When was this document
232 modified?</title>
233
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>
240 <example>
241         &lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
242         This file last modified &lt;!--#flastmod file="ssi.shtml" --&gt;
243 </example>
244
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>
251 <example>
252         &lt;!--#config timefmt="%D" --&gt;<br />
253         This file last modified &lt;!--#echo var="LAST_MODIFIED" --&gt;
254 </example>
255
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>
259 </section>
260
261 <section id="standard-footer">
262 <title>Including a standard footer</title>
263
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>
268
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>
282 <example>
283         &lt;!--#include virtual="/footer.html" --&gt;
284 </example>
285
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>
291 </section>
292
293 </section>
294
295 <section id="config">
296 <title>What else can I config?</title>
297
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>
300
301     <p>Usually, when something goes wrong with your SSI directive,
302     you get the message</p>
303 <example>
304         [an error occurred while processing this directive]
305 </example>
306
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>
310 <example>
311         &lt;!--#config errmsg="[It appears that you don't know how to use SSI]" --&gt;
312 </example>
313
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>
317
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
322     appropriate.</p>
323     </section>
324
325 <section id="exec">
326     <title>Executing commands</title>
327
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>
335 <example>
336         &lt;pre&gt;<br />
337         &lt;!--#exec cmd="ls" --&gt;<br />
338         &lt;/pre&gt;
339 </example>
340
341     <p>or, on Windows</p>
342 <example>
343         &lt;pre&gt;<br />
344         &lt;!--#exec cmd="dir" --&gt;<br />
345         &lt;/pre&gt;
346 </example>
347
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 ``&lt;<code>dir</code>&gt;'' in it, which confuses
351     browsers.</p>
352
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>
361     </section>
362
363 <section id="advanced">
364 <title>Advanced SSI techniques</title>
365
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>
369
370 <section id="variables"><title>Setting variables</title>
371
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>
375 <example>
376         &lt;!--#set var="name" value="Rich" --&gt;
377 </example>
378
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> 
386
387     <example> &lt;!--#set var="modified" value="$LAST_MODIFIED" --&gt;
388     </example>
389
390     <p>To put a literal dollar sign into the value of your
391     variable, you need to escape the dollar sign with a
392     backslash.</p>
393 <example>
394         &lt;!--#set var="cost" value="\$100" --&gt;
395 </example>
396
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 <example>
405         &lt;!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --&gt;
406 </example>
407 </section>
408
409 <section id="conditional">
410 <title>Conditional expressions</title>
411
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
419     actual page.</p>
420
421     <p>The structure of this conditional construct is:</p>
422 <example>
423     &lt;!--#if expr="test_condition" --&gt;<br />
424     &lt;!--#elif expr="test_condition" --&gt;<br />
425     &lt;!--#else --&gt;<br />
426     &lt;!--#endif --&gt;
427 </example>
428
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
435     construct.</p>
436
437     <p>In your configuration file, you could put the following
438     line:</p>
439 <example>
440         BrowserMatchNoCase macintosh Mac<br />
441         BrowserMatchNoCase MSIE InternetExplorer
442 </example>
443
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>
447
448     <p>Then, in your SSI-enabled document, you might do the
449     following:</p>
450 <example>
451         &lt;!--#if expr="${Mac} &amp;&amp; ${InternetExplorer}" --&gt;<br />
452         Apologetic text goes here<br />
453         &lt;!--#else --&gt;<br />
454         Cool JavaScript code goes here<br />
455         &lt;!--#endif --&gt;
456 </example>
457
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>
462
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>
469 </section>
470 </section>
471
472 <section id="conclusion"><title>Conclusion</title>
473
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>
478 </section>
479
480 </manualpage>