📄 ssi.html.en
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><title>Apache Tutorial: Introduction to Server Side Includes</title><link rev="made" href="mailto:rbowen@rcbowen.com"></head><!-- Background white, links blue (unvisited), navy (visited), red (active) --><body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#000080"alink="#FF0000"><!--#include virtual="header.html" --><h1 align="CENTER">Apache Tutorial: Introduction to Server SideIncludes</h1><a name="__index__"></a> <!-- INDEX BEGIN --> <ul><li><a href="#apachetutorial:introductiontoserversideincludes">ApacheTutorial: Introduction to Server Side Includes</a></li><li><a href="#whataressi">What are SSI?</a></li><li><a href="#configuringyourservertopermitssi">Configuring yourserver to permit SSI</a></li><li><a href="#basicssidirectives">Basic SSI directives</a><ul> <li><a href="#today'sdate">Today's date</a></li> <li><a href="#modificationdateofthefile">Modification date of thefile</a></li> <li><a href="#includingtheresultsofacgiprogram">Including theresults of a CGI program</a></li></ul></li><li><a href="#additionalexamples">Additional examples</a><ul><li><a href="#whenwasthisdocumentmodified">When was this documentmodified?</a></li><li><a href="#includingastandardfooter">Including a standardfooter</a></li><li><a href="#whatelsecaniconfig">What else can I config?</a></li><li><a href="#executingcommands">Executing commands</a></li></ul></li><li><a href="#advancedssitechniques">Advanced SSI techniques</a><ul><li><a href="#settingvariables">Setting variables</a></li><li><a href="#conditionalexpressions">Conditional expressions</a></li></ul></li><li><a href="#conclusion">Conclusion</a></li></ul><!-- INDEX END --><hr><h2><a name="apachetutorial:introductiontoserversideincludes">ApacheTutorial: Introduction to Server Side Includes</a></h2><table border="1"><tr><td valign="top"><strong>Related Modules</strong><br><br> <a href="../mod/mod_include.html">mod_include</a><br><a href="../mod/mod_cgi.html">mod_cgi</a><br><a href="../mod/mod_expires.html">mod_expires</a><br> </td><td valign="top"><strong>Related Directives</strong><br><br> <a href="../mod/core.html#options">Options</a><br><a href="../mod/mod_include.html#xbithack">XBitHack</a><br><a href="../mod/mod_mime.html#addtype">AddType</a><br><a href="../mod/mod_mime.html#addhandler">AddHandler</a><br><a href="../mod/mod_setenvif.html#BrowserMatchNoCase">BrowserMatchNoCase</a><br> </td></tr></table><p>This HOWTO first appeared in Apache Today(http://www.apachetoday.com/) as a series of three articles. Theyappear here by arrangement with ApacheToday and Internet.com.</p><p>This article deals with Server Side Includes, usually called simplySSI. In this article, I'll talk about configuring your server to permitSSI, and introduce some basic SSI techniques for adding dynamic contentto your existing HTML pages.</p><p>In the latter part of the article, we'll talk about some of thesomewhat more advanced things that can be done with SSI, such asconditional statements in your SSI directives.</p><hr><h2><a name="whataressi">What are SSI?</a></h2><p>SSI (Server Side Includes) are directives that are placed in HTMLpages, and evaluated on the server while the pages are being served.They let you add dynamically generated content to an existing HTMLpage, without having to serve the entire page via a CGI program, orother dynamic technology.</p><p>The decision of when to use SSI, and when to have your page entirelygenerated by some program, is usually a matter of how much of the pageis static, and how much needs to be recalculated every time the page isserved. SSI is a great way to add small pieces of information, such asthe current time. But if a majority of your page is being generated atthe time that it is served, you need to look for some othersolution.</p><hr><h2><a name="configuringyourservertopermitssi">Configuring yourserver to permit SSI</a></h2><p>To permit SSI on your server, you must have the following directiveeither in your <code>httpd.conf</code> file, or in a<code>.htaccess</code> file:</p><pre> Options +Includes</pre><p>This tells Apache that you want to permit files to be parsed for SSIdirectives.</p><p>Not just any file is parsed for SSI directives. You have to tellApache which files should be parsed. There are two ways to do this. Youcan tell Apache to parse any file with a particular file extension,such as <code>.shtml</code>, with the following directives:</p><pre> AddType text/html .shtml <FilesMatch "\.shtml[.$]"> SetOutputFilter INCLUDES<br> </FilesMatch></pre><p>One disadvantage to this approach is that if you wanted to add SSIdirectives to an existing page, you would have to change the name ofthat page, and all links to that page, in order to give it a<code>.shtml</code> extension, so that those directives would beexecuted.</p><p>The other method is to use the <code>XBitHack</code> directive:</p><pre> XBitHack on</pre><p><code>XBitHack</code> tells Apache to parse files for SSI directivesif they have the execute bit set. So, to add SSI directives to anexisting page, rather than having to change the file name, you wouldjust need to make the file executable using <code>chmod</code>.</p><pre> chmod +x pagename.html</pre><p>A brief comment about what not to do. You'll occasionally see peoplerecommending that you just tell Apache to parse all <code>.html</code>files for SSI, so that you don't have to mess with <code>.shtml</code>file names. These folks have perhaps not heard about<code>XBitHack</code>. The thing to keep in mind is that, by doingthis, you're requiring that Apache read through every single file thatit sends out to clients, even if they don't contain any SSI directives.This can slow things down quite a bit, and is not a good idea.</p><p>Of course, on Windows, there is no such thing as an execute bit toset, so that limits your options a little.</p><p>In its default configuration, Apache does not send the last modifieddate or content length HTTP headers on SSI pages, because these values aredifficult to calculate for dynamic content. This can prevent yourdocument from being cached, and result in slower perceived clientperformance. There are two ways to solve this:</p><ol> <li>Use the <code>XBitHack Full</code> configuration. This tellsApache to determine the last modified date by looking only at the dateof the originally requested file, ignoring the modification date ofany included files. </li> <li>Use the directives provided by <ahref="../mod/mod_expires.html">mod_expires</a> to set an explicitexpiration time on your files, thereby letting browsers and proxiesknow that it is acceptable to cache them. </li></ol><hr><h2><a name="basicssidirectives">Basic SSI directives</a></h2><p>SSI directives have the following syntax:</p><pre> <!--#element attribute=value attribute=value ... --></pre><p>It is formatted like an HTML comment, so if you don't have SSIcorrectly enabled, the browser will ignore it, but it will still bevisible in the HTML source. If you have SSI correctly configured, thedirective will be replaced with its results.</p><p>The element can be one of a number of things, and we'll talk somemore about most of these in the next installment of this series. Fornow, here are some examples of what you can do with SSI</p><h3><a name="today'sdate">Today's date</a></h3><pre> <!--#echo var="DATE_LOCAL" --></pre><p>The <code>echo</code> element just spits out the value of avariable. There are a number of standard variables, which include thewhole 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 canuse the <code>config</code> element, with a <code>timefmt</code>attribute, to modify that formatting.</p><pre> <!--#config timefmt="%A %B %d, %Y" --> Today is <!--#echo var="DATE_LOCAL" --></pre><h3><a name="modificationdateofthefile">Modification date of thefile</a></h3><pre> This document last modified <!--#flastmod file="index.html" --></pre><p>This element is also subject to <code>timefmt</code> formatconfigurations.</p><h3><a name="includingtheresultsofacgiprogram">Including theresults of a CGI program</a></h3><p>This is one of the more common uses of SSI - to output the resultsof a CGI program, such as everybody's favorite, a ``hit counter.''</p><pre> <!--#include virtual="/cgi-bin/counter.pl" -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -