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