]> granicus.if.org Git - apache/blob - docs/manual/howto/ssi.xml
Remove useless <br \> in highlight blocks.
[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 httpd 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>For example, you might place a directive into an existing HTML
70     page, such as:</p>
71
72     <example>
73     &lt;!--#echo var="DATE_LOCAL" --&gt;
74     </example>
75
76     <p>And, when the page is served, this fragment will be evaluated and replaced with its value:</p>
77
78     <example>
79     Tuesday, 15-Jan-2013 19:28:54 EST
80     </example>
81
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>
89 </section>
90
91 <section id="configuring">
92 <title>Configuring your server to permit SSI</title>
93
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">
98 Options +Includes
99 </highlight>
100
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>
107
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
116 </highlight>
117
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>
123
124     <p>The other method is to use the <directive
125     module="mod_include">XBitHack</directive> directive:</p>
126 <highlight language="config">
127 XBitHack on
128 </highlight>
129
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>
136 <example>
137         chmod +x pagename.html
138 </example>
139
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>
150
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>
153
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>
160
161     <ol>
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>
166
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>
171     </ol>
172 </section>
173
174 <section id="basic"><title>Basic SSI directives</title>
175
176     <p>SSI directives have the following syntax:</p>
177 <example>
178         &lt;!--#function attribute=value attribute=value ... --&gt;
179 </example>
180
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
185     results.</p>
186
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
190     SSI</p>
191
192 <section id="todaysdate"><title>Today's date</title>
193
194 <example>
195         &lt;!--#echo var="DATE_LOCAL" --&gt;
196 </example>
197
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>
203
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>
207
208 <example>
209         &lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
210         Today is &lt;!--#echo var="DATE_LOCAL" --&gt;
211 </example>
212 </section>
213
214 <section id="lastmodified"><title>Modification date of the file</title>
215
216 <example>
217         This document last modified &lt;!--#flastmod file="index.html" --&gt;
218 </example>
219
220     <p>This function is also subject to <code>timefmt</code> format
221     configurations.</p>
222 </section>
223
224 <section id="cgi"><title>Including the results of a CGI program</title>
225
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
228     counter.''</p>
229
230 <example>
231         &lt;!--#include virtual="/cgi-bin/counter.pl" --&gt;
232 </example>
233
234 </section>
235 </section>
236
237 <section id="additionalexamples">
238 <title>Additional examples</title>
239
240     <p>Following are some specific examples of things you can do in
241     your HTML documents with SSI.</p>
242
243 <section id="docmodified"><title>When was this document
244 modified?</title>
245
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>
252 <example>
253         &lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
254         This file last modified &lt;!--#flastmod file="ssi.shtml" --&gt;
255 </example>
256
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>
263 <example>
264         &lt;!--#config timefmt="%D" --&gt;<br />
265         This file last modified &lt;!--#echo var="LAST_MODIFIED" --&gt;
266 </example>
267
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>
271 </section>
272
273 <section id="standard-footer">
274 <title>Including a standard footer</title>
275
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>
280
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>
294 <example>
295         &lt;!--#include virtual="/footer.html" --&gt;
296 </example>
297
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>
303 </section>
304
305 </section>
306
307 <section id="config">
308 <title>What else can I config?</title>
309
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>
312
313     <p>Usually, when something goes wrong with your SSI directive,
314     you get the message</p>
315 <example>
316         [an error occurred while processing this directive]
317 </example>
318
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>
322 <example>
323         &lt;!--#config errmsg="[It appears that you don't know how to use SSI]" --&gt;
324 </example>
325
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>
329
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
334     appropriate.</p>
335     </section>
336
337 <section id="exec">
338     <title>Executing commands</title>
339
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>
347 <example>
348         &lt;pre&gt;<br />
349         &lt;!--#exec cmd="ls" --&gt;<br />
350         &lt;/pre&gt;
351 </example>
352
353     <p>or, on Windows</p>
354 <example>
355         &lt;pre&gt;<br />
356         &lt;!--#exec cmd="dir" --&gt;<br />
357         &lt;/pre&gt;
358 </example>
359
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 ``&lt;<code>dir</code>&gt;'' in it, which confuses
363     browsers.</p>
364
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>
373     </section>
374
375 <section id="advanced">
376 <title>Advanced SSI techniques</title>
377
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>
381
382 <section id="variables"><title>Setting variables</title>
383
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>
387 <example>
388         &lt;!--#set var="name" value="Rich" --&gt;
389 </example>
390
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>
398
399     <example> &lt;!--#set var="modified" value="$LAST_MODIFIED" --&gt;
400     </example>
401
402     <p>To put a literal dollar sign into the value of your
403     variable, you need to escape the dollar sign with a
404     backslash.</p>
405 <example>
406         &lt;!--#set var="cost" value="\$100" --&gt;
407 </example>
408
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>
416 <example>
417         &lt;!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --&gt;
418 </example>
419 </section>
420
421 <section id="conditional">
422 <title>Conditional expressions</title>
423
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
431     actual page.</p>
432
433     <p>The structure of this conditional construct is:</p>
434 <example>
435     &lt;!--#if expr="test_condition" --&gt;<br />
436     &lt;!--#elif expr="test_condition" --&gt;<br />
437     &lt;!--#else --&gt;<br />
438     &lt;!--#endif --&gt;
439 </example>
440
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>
446     documentation.</p>
447    
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>
451
452     <example>
453     Good
454     &lt;!--#if expr="%{TIME_HOUR} &lt;12" --&gt;<br />
455     morning!<br />
456     &lt;!--#else --&gt;<br />
457     afternoon!<br />
458     &lt;!--#endif --&gt;<br />
459     </example>
460
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>
465
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>
470 </section>
471 </section>
472
473 <section id="conclusion"><title>Conclusion</title>
474
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 </section>
480
481 </manualpage>