ssi.html.en revision 93a89127fafc9d64767c71d81f667bf5e31c2cfd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<?xml version="1.0" encoding="ISO-8859-1"?>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd This file is generated from xml source: DO NOT EDIT
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd -->
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<title>Apache Tutorial: Introduction to Server Side Includes - Apache HTTP Server</title>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<link href="/images/favicon.ico" rel="shortcut icon" /></head>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<body id="manual-page"><div id="page-header">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<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>
27e52281f1522522b170cafc76b08b58aa70ccaand<p class="apache">Apache HTTP Server Version 2.1</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<img alt="" src="/images/feather.gif" /></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div id="path">
4b5981e276e93df97c34e4da05ca5cf8bbd937dand<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-project/">Documentation</a> &gt; <a href="../">Version 2.1</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>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<p>Server-side includes provide a means to add dynamic content to
a34d44ce259e68932a3db730a7cb0d3edde46860kessexisting HTML documents.</p>
a34d44ce259e68932a3db730a7cb0d3edde46860kess</div>
43e2c3ca329ff3ca8cb7613d455ddb3794edf7adnd<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#related">Introduction</a></li>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<li><img alt="" src="/images/down.gif" /> <a href="#what">What are SSI?</a></li>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<li><img alt="" src="/images/down.gif" /> <a href="#configuring">Configuring your server to permit SSI</a></li>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<li><img alt="" src="/images/down.gif" /> <a href="#basic">Basic SSI directives</a></li>
9bcfc3697a91b5215893a7d0206865b13fc72148nd<li><img alt="" src="/images/down.gif" /> <a href="#additionalexamples">Additional examples</a></li>
9bcfc3697a91b5215893a7d0206865b13fc72148nd<li><img alt="" src="/images/down.gif" /> <a href="#config">What else can I config?</a></li>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<li><img alt="" src="/images/down.gif" /> <a href="#exec">Executing commands</a></li>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<li><img alt="" src="/images/down.gif" /> <a href="#advanced">Advanced SSI techniques</a></li>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<li><img alt="" src="/images/down.gif" /> <a href="#conclusion">Conclusion</a></li>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</ul></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="section">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<h2><a name="related" id="related">Introduction</a></h2>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <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>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>This article deals with Server Side Includes, usually called
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd simply SSI. In this article, I'll talk about configuring your
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak server to permit SSI, and introduce some basic SSI techniques
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd for adding dynamic content to your existing HTML pages.</p>
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak <p>In the latter part of the article, we'll talk about some of
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd the somewhat more advanced things that can be done with SSI,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd such as conditional statements in your SSI directives.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="section">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<h2><a name="what" id="what">What are SSI?</a></h2>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>SSI (Server Side Includes) are directives that are placed in
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd HTML pages, and evaluated on the server while the pages are
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd being served. They let you add dynamically generated content to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd an existing HTML page, without having to serve the entire page
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak via a CGI program, or other dynamic technology.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>The decision of when to use SSI, and when to have your page
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd entirely generated by some program, is usually a matter of how
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd much of the page is static, and how much needs to be
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd recalculated every time the page is served. SSI is a great way
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd to add small pieces of information, such as the current time.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd But if a majority of your page is being generated at the time
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd that it is served, you need to look for some other
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd solution.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="section">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<h2><a name="configuring" id="configuring">Configuring your server to permit SSI</a></h2>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>To permit SSI on your server, you must have the following
0a336dd9ddab495062363310a4a4d8a0fb69c495nd directive either in your <code>httpd.conf</code> file, or in a
0a336dd9ddab495062363310a4a4d8a0fb69c495nd <code>.htaccess</code> file:</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="example"><p><code>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd Options +Includes
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</code></p></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>This tells Apache that you want to permit files to be parsed
5383fa753e1bcd3a04ec34ba9810d671302380f2colm for SSI directives. Note that most configurations contain
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd multiple <code class="directive"><a href="/mod/core.html#options">Options</a></code> directives
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd that can override each other. You will probably need to apply the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <code>Options</code> to the specific directory where you want SSI
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd enabled in order to assure that it gets evaluated last.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>Not just any file is parsed for SSI directives. You have to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd tell Apache which files should be parsed. There are two ways to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd do this. You can tell Apache to parse any file with a
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd particular file extension, such as <code>.shtml</code>, with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd the following directives:</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="example"><p><code>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd AddType text/html .shtml<br />
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd AddOutputFilter INCLUDES .shtml
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</code></p></div>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>One disadvantage to this approach is that if you wanted to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd add SSI directives to an existing page, you would have to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd change the name of that page, and all links to that page, in
71fccc298df6a1540d408151a26aa22beed55d0bnd order to give it a <code>.shtml</code> extension, so that those
71fccc298df6a1540d408151a26aa22beed55d0bnd directives would be executed.</p>
71fccc298df6a1540d408151a26aa22beed55d0bnd
71fccc298df6a1540d408151a26aa22beed55d0bnd <p>The other method is to use the <code class="directive"><a href="/mod/mod_include.html#xbithack">XBitHack</a></code> directive:</p>
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak<div class="example"><p><code>
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak XBitHack on
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</code></p></div>
71fccc298df6a1540d408151a26aa22beed55d0bnd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p><code class="directive"><a href="/mod/mod_include.html#xbithack">XBitHack</a></code>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd tells Apache to parse files for SSI
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd directives if they have the execute bit set. So, to add SSI
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd directives to an existing page, rather than having to change
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak the file name, you would just need to make the file executable
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd using <code>chmod</code>.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="example"><p><code>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd chmod +x pagename.html
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</code></p></div>
0a336dd9ddab495062363310a4a4d8a0fb69c495nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>A brief comment about what not to do. You'll occasionally
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd see people recommending that you just tell Apache to parse all
71fccc298df6a1540d408151a26aa22beed55d0bnd <code>.html</code> files for SSI, so that you don't have to
71fccc298df6a1540d408151a26aa22beed55d0bnd mess with <code>.shtml</code> file names. These folks have
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak perhaps not heard about <code class="directive"><a href="/mod/mod_include.html#xbithack">XBitHack</a></code>. The thing to
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak keep in mind is that, by doing this, you're requiring that
71fccc298df6a1540d408151a26aa22beed55d0bnd Apache read through every single file that it sends out to
71fccc298df6a1540d408151a26aa22beed55d0bnd clients, even if they don't contain any SSI directives. This
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd can slow things down quite a bit, and is not a good idea.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>Of course, on Windows, there is no such thing as an execute
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd bit to set, so that limits your options a little.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>In its default configuration, Apache does not send the last
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak modified date or content length HTTP headers on SSI pages,
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak because these values are difficult to calculate for dynamic
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd content. This can prevent your document from being cached, and
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd result in slower perceived client performance. There are two
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ways to solve this:</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <ol>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <li>Use the <code>XBitHack Full</code> configuration. This
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd tells Apache to determine the last modified date by looking
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd only at the date of the originally requested file, ignoring
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd the modification date of any included files.</li>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <li>Use the directives provided by
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <code class="module"><a href="/mod/mod_expires.html">mod_expires</a></code> to set an explicit expiration
a34d44ce259e68932a3db730a7cb0d3edde46860kess time on your files, thereby letting browsers and proxies
a34d44ce259e68932a3db730a7cb0d3edde46860kess know that it is acceptable to cache them.</li>
43e2c3ca329ff3ca8cb7613d455ddb3794edf7adnd </ol>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<div class="section">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<h2><a name="basic" id="basic">Basic SSI directives</a></h2>
4b22542f6f38567cae7873b176188a6622f67eb0fielding
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>SSI directives have the following syntax:</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<div class="example"><p><code>
&lt;!--#element attribute=value attribute=value ... --&gt;
</code></p></div>
<p>It is formatted like an HTML comment, so if you don't have
SSI correctly enabled, the browser will ignore it, but it will
still be visible in the HTML source. If you have SSI correctly
configured, the directive will be replaced with its
results.</p>
<p>The element can be one of a number of things, and we'll talk
some more about most of these in the next installment of this
series. For now, here are some examples of what you can do with
SSI</p>
<h3><a name="todaysdate" id="todaysdate">Today's date</a></h3>
<div class="example"><p><code>
&lt;!--#echo var="DATE_LOCAL" --&gt;
</code></p></div>
<p>The <code>echo</code> element just spits out the value of a
variable. There are a number of standard variables, which
include the whole set of environment variables that are
available to CGI programs. Also, you can define your own
variables with the <code>set</code> element.</p>
<p>If you don't like the format in which the date gets printed,
you can use the <code>config</code> element, with a
<code>timefmt</code> attribute, to modify that formatting.</p>
<div class="example"><p><code>
&lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
Today is &lt;!--#echo var="DATE_LOCAL" --&gt;
</code></p></div>
<h3><a name="lastmodified" id="lastmodified">Modification date of the file</a></h3>
<div class="example"><p><code>
This document last modified &lt;!--#flastmod file="index.html" --&gt;
</code></p></div>
<p>This element is also subject to <code>timefmt</code> format
configurations.</p>
<h3><a name="cgi" id="cgi">Including the results of a CGI program</a></h3>
<p>This is one of the more common uses of SSI - to output the
results of a CGI program, such as everybody's favorite, a ``hit
counter.''</p>
<div class="example"><p><code>
&lt;!--#include virtual="/cgi-bin/counter.pl" --&gt;
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="additionalexamples" id="additionalexamples">Additional examples</a></h2>
<p>Following are some specific examples of things you can do in
your HTML documents with SSI.</p>
<h3><a name="docmodified" id="docmodified">When was this document
modified?</a></h3>
<p>Earlier, we mentioned that you could use SSI to inform the
user when the document was most recently modified. However, the
actual method for doing that was left somewhat in question. The
following code, placed in your HTML document, will put such a
time stamp on your page. Of course, you will have to have SSI
correctly enabled, as discussed above.</p>
<div class="example"><p><code>
&lt;!--#config timefmt="%A %B %d, %Y" --&gt;<br />
This file last modified &lt;!--#flastmod file="ssi.shtml" --&gt;
</code></p></div>
<p>Of course, you will need to replace the
<code>ssi.shtml</code> with the actual name of the file that
you're referring to. This can be inconvenient if you're just
looking for a generic piece of code that you can paste into any
file, so you probably want to use the
<code>LAST_MODIFIED</code> variable instead:</p>
<div class="example"><p><code>
&lt;!--#config timefmt="%D" --&gt;<br />
This file last modified &lt;!--#echo var="LAST_MODIFIED" --&gt;
</code></p></div>
<p>For more details on the <code>timefmt</code> format, go to
your favorite search site and look for <code>strftime</code>. The
syntax is the same.</p>
<h3><a name="standard-footer" id="standard-footer">Including a standard footer</a></h3>
<p>If you are managing any site that is more than a few pages,
you may find that making changes to all those pages can be a
real pain, particularly if you are trying to maintain some kind
of standard look across all those pages.</p>
<p>Using an include file for a header and/or a footer can
reduce the burden of these updates. You just have to make one
footer file, and then include it into each page with the
<code>include</code> SSI command. The <code>include</code>
element can determine what file to include with either the
<code>file</code> attribute, or the <code>virtual</code>
attribute. The <code>file</code> attribute is a file path,
<em>relative to the current directory</em>. That means that it
cannot be an absolute file path (starting with /), nor can it
contain ../ as part of that path. The <code>virtual</code>
attribute is probably more useful, and should specify a URL
relative to the document being served. It can start with a /,
but must be on the same server as the file being served.</p>
<div class="example"><p><code>
&lt;!--#include virtual="/footer.html" --&gt;
</code></p></div>
<p>I'll frequently combine the last two things, putting a
<code>LAST_MODIFIED</code> directive inside a footer file to be
included. SSI directives can be contained in the included file,
and includes can be nested - that is, the included file can
include another file, and so on.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="config" id="config">What else can I config?</a></h2>
<p>In addition to being able to <code>config</code> the time
format, you can also <code>config</code> two other things.</p>
<p>Usually, when something goes wrong with your SSI directive,
you get the message</p>
<div class="example"><p><code>
[an error occurred while processing this directive]
</code></p></div>
<p>If you want to change that message to something else, you
can do so with the <code>errmsg</code> attribute to the
<code>config</code> element:</p>
<div class="example"><p><code>
&lt;!--#config errmsg="[It appears that you don't know how to use SSI]" --&gt;
</code></p></div>
<p>Hopefully, end users will never see this message, because
you will have resolved all the problems with your SSI
directives before your site goes live. (Right?)</p>
<p>And you can <code>config</code> the format in which file
sizes are returned with the <code>sizefmt</code> attribute. You
can specify <code>bytes</code> for a full count in bytes, or
<code>abbrev</code> for an abbreviated number in Kb or Mb, as
appropriate.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="exec" id="exec">Executing commands</a></h2>
<p>I expect that I'll have an article some time in the coming
months about using SSI with small CGI programs. For now, here's
something else that you can do with the <code>exec</code>
element. You can actually have SSI execute a command using the
shell (<code>/bin/sh</code>, to be precise - or the DOS shell,
if you're on Win32). The following, for example, will give you
a directory listing.</p>
<div class="example"><p><code>
&lt;pre&gt;<br />
&lt;!--#exec cmd="ls" --&gt;<br />
&lt;/pre&gt;
</code></p></div>
<p>or, on Windows</p>
<div class="example"><p><code>
&lt;pre&gt;<br />
&lt;!--#exec cmd="dir" --&gt;<br />
&lt;/pre&gt;
</code></p></div>
<p>You might notice some strange formatting with this directive
on Windows, because the output from <code>dir</code> contains
the string ``&lt;<code>dir</code>&gt;'' in it, which confuses
browsers.</p>
<p>Note that this feature is exceedingly dangerous, as it will
execute whatever code happens to be embedded in the
<code>exec</code> tag. If you have any situation where users
can edit content on your web pages, such as with a
``guestbook'', for example, make sure that you have this
feature disabled. You can allow SSI, but not the
<code>exec</code> feature, with the <code>IncludesNOEXEC</code>
argument to the <code>Options</code> directive.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="advanced" id="advanced">Advanced SSI techniques</a></h2>
<p>In addition to spitting out content, Apache SSI gives you
the option of setting variables, and using those variables in
comparisons and conditionals.</p>
<h3><a name="caveat" id="caveat">Caveat</a></h3>
<p>Most of the features discussed in this article are only
available to you if you are running Apache 1.2 or later. Of
course, if you are not running Apache 1.2 or later, you need to
upgrade immediately, if not sooner. Go on. Do it now. We'll
wait.</p>
<h3><a name="variables" id="variables">Setting variables</a></h3>
<p>Using the <code>set</code> directive, you can set variables
for later use. We'll need this later in the discussion, so
we'll talk about it here. The syntax of this is as follows:</p>
<div class="example"><p><code>
&lt;!--#set var="name" value="Rich" --&gt;
</code></p></div>
<p>In addition to merely setting values literally like that,
you can use any other variable, including, for example,
environment variables, or some of the variables we discussed in
the last article (like <code>LAST_MODIFIED</code>, for example)
to give values to your variables. You will specify that
something is a variable, rather than a literal string, by using
the dollar sign ($) before the name of the variable.</p>
<div class="example"><p><code>
&lt;!--#set var="modified" value="$LAST_MODIFIED" --&gt;
</code></p></div>
<p>To put a literal dollar sign into the value of your
variable, you need to escape the dollar sign with a
backslash.</p>
<div class="example"><p><code>
&lt;!--#set var="cost" value="\$100" --&gt;
</code></p></div>
<p>Finally, if you want to put a variable in the midst of a
longer string, and there's a chance that the name of the
variable will run up against some other characters, and thus be
confused with those characters, you can place the name of the
variable in braces, to remove this confusion. (It's hard to
come up with a really good example of this, but hopefully
you'll get the point.)</p>
<div class="example"><p><code>
&lt;!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --&gt;
</code></p></div>
<h3><a name="conditional" id="conditional">Conditional expressions</a></h3>
<p>Now that we have variables, and are able to set and compare
their values, we can use them to express conditionals. This
lets SSI be a tiny programming language of sorts.
<code class="module"><a href="/mod/mod_include.html">mod_include</a></code> provides an <code>if</code>,
<code>elif</code>, <code>else</code>, <code>endif</code>
structure for building conditional statements. This allows you
to effectively generate multiple logical pages out of one
actual page.</p>
<p>The structure of this conditional construct is:</p>
<div class="example"><p><code>
&lt;!--#if expr="test_condition" --&gt;<br />
&lt;!--#elif expr="test_condition" --&gt;<br />
&lt;!--#else --&gt;<br />
&lt;!--#endif --&gt;
</code></p></div>
<p>A <em>test_condition</em> can be any sort of logical
comparison - either comparing values to one another, or testing
the ``truth'' of a particular value. (A given string is true if
it is nonempty.) For a full list of the comparison operators
available to you, see the <code class="module"><a href="/mod/mod_include.html">mod_include</a></code>
documentation. Here are some examples of how one might use this
construct.</p>
<p>In your configuration file, you could put the following
line:</p>
<div class="example"><p><code>
BrowserMatchNoCase macintosh Mac<br />
BrowserMatchNoCase MSIE InternetExplorer
</code></p></div>
<p>This will set environment variables ``Mac'' and
``InternetExplorer'' to true, if the client is running Internet
Explorer on a Macintosh.</p>
<p>Then, in your SSI-enabled document, you might do the
following:</p>
<div class="example"><p><code>
&lt;!--#if expr="${Mac} &amp;&amp; ${InternetExplorer}" --&gt;<br />
Apologetic text goes here<br />
&lt;!--#else --&gt;<br />
Cool JavaScript code goes here<br />
&lt;!--#endif --&gt;
</code></p></div>
<p>Not that I have anything against IE on Macs - I just
struggled for a few hours last week trying to get some
JavaScript working on IE on a Mac, when it was working
everywhere else. The above was the interim workaround.</p>
<p>Any other variable (either ones that you define, or normal
environment variables) can be used in conditional statements.
With Apache's ability to set environment variables with the
<code>SetEnvIf</code> directives, and other related directives,
this functionality can let you do some pretty involved dynamic
stuff without ever resorting to CGI.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="conclusion" id="conclusion">Conclusion</a></h2>
<p>SSI is certainly not a replacement for CGI, or other
technologies used for generating dynamic web pages. But it is a
great way to add small amounts of dynamic content to pages,
without doing a lot of extra work.</p>
</div></div>
<div id="footer">
<p class="apache">Maintained by the <a href="http://httpd.apache.org/docs-project/">Apache HTTP Server Documentation Project</a></p>
<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>
</body></html>