Link to USGS home page
Internal USGS Access Only

Server Side Includes

Server-side includes are html comments containing special flags which cause the server to substitute into the html document before displaying it.

Server Side Includes are not standardized.
The syntax and capabilities vary with the http (web-server) software used.

The following examples are for the Apache Web Server:

The "if" directive controls when content is displayed

Below is an example of SSI code to restrict a portion of a paragraph for USGS-only display and another portion for non-USGS only display.

<!--#if expr="${REMOTE_HOST} = /.usgs.gov$/ || ${REMOTE_HOST} = /.gs.doi.net$/" -->
  <p><b>Display this text to internal user's only!</b></p>
<!--#endif -->

[an error occurred while processing this directive]

Display this text to internal user's only!

An example of how to detect the user's browser:

<!--#if expr="${HTTP_USER_AGENT} = /Firefox/" -->
<p><b>You are using FireFox.</b></p>
<!--#else -->
<p><b>You are not using FireFox.</b></p>
<!--#endif -->

[an error occurred while processing this directive]

You are using FireFox


The "echo" directive displays environment variables

Below are a few of the available variables.

<!--#echo var="DATE_GMT" -->
Thursday, 18-Jun-2026 03:18:55 GMT

<!--#echo var="DOCUMENT_NAME" -->
ssi.html

<!--#echo var="DOCUMENT_URI" -->
/usgs/training/webmaster/ssi.html

<!--#echo var="LAST_MODIFIED" -->
Tuesday, 13-Dec-2016 11:54:29 EST

<!--#echo var="DOCUMENT_ROOT" -->
/afs/usgs.gov/www/water/htdocs

<!--#echo var="HTTP_USER_AGENT" -->
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

<!--#echo var="HTTP_HOST" -->
water.usgs.gov

<!--#echo var="SERVER_NAME" -->
water.usgs.gov

<!--#echo var="REMOTE_HOST" -->
(none)

<!--#echo var="REMOTE_ADDR" -->
137.227.229.2

<!--#echo var="SERVER_ADMIN" -->
h2oteam@usgs.gov


The "exec" directive executes a command and displays the results

<!--#exec cmd="/usr/bin/date" -->
Wed Jun 17 23:18:55 EDT 2026

<pre><!--#exec cmd="/usr/bin/hostname" --></pre>
IGSAWS910SVPWE1.GS.DOI.NET

<pre><!--#exec cmd="/usr/bin/prstat 1 1 | /usr/bin/head -6" --></pre>

	


The "include" directive includes the contents of a file

<!--#include virtual="example/sample.txt" -->
This is just a file with some text in it.

<!--#include file="example/sample.txt" -->
This is just a file with some text in it.

Virtual uses paths as a web browser sees them (starting from DocumentRoot), and file can see the full Unix directory structure. Virtual is encouraged for including files found in the web directory.

The extension .htmli is often used to distinguish include files, which include html code but are not complete html documents, from text files.

Note: In Apache 2.0, virtual becomes recursive, that is, included files can also have includes.


The "flastmod" and "fsize" directives return the modification date and size of a file

<!--#flastmod virtual="example/sample.txt" -->
Friday, 10-Sep-1999 16:11:23 EDT
<!--#fsize virtual="example/sample.txt" -->
46

The "config" directive changes the format of date or file size information

<!--#echo var="LAST_MODIFIED" -->
Tuesday, 13-Dec-2016 11:54:29 EST
<!--#config timefmt="%d %b %Y" -->
<!--#echo var="LAST_MODIFIED" -->
13 Dec 2016
<!--#config timefmt="%I:%M %p on %A, %B %d, %Y" -->
<!--#echo var="LAST_MODIFIED" -->
11:54 AM on Tuesday, December 13, 2016

The date format options can be seen using the Unix command: man strftime; You can skip first section describing the C library function.


To enable Server Side Includes

On Sun, edit one file /usr/local/apache/etc/httpd.conf to change the same options as above; Options and AddType.

Only files with the suffixes defined by AddType will be parsed for SSIs! If you Include any files which in turn contain SSIs, these also must have an appropriate file suffix.

A common use of SSI is to share a bit of html code among many files, such as banner or footer information, with a simple include.

Programs invoked through exec can range from very simple to very complex. An example of SSI programming is a self-expiring "new" icon New that goes away when a time limit is exceeded. Programs executed from a server-side include have access to the same environment variables as those using CGI.

Another useful SSI program is classgrep, which can extract portions of other documents on your web site and include them on any number of other pages, or documents. For example, <!--#classgrep alert virtual="/news.html"--> will extract the contents of any tag with the attribute "alert" found in the news.html file and insert it in place of the SSI. You can also use DIV tags to surround as much html code as you like. It even has an option for random selections by using a -r option following the classgrep command.(use classgrep -r class virtual="/news.html"> for one item to be selected, or -r2, or -r3 .

Server-Side Includes can be turned on or off, or may be on but have "exec" disabled.

The official description of Apache SSI is available on the Apache Web site: http://httpd.apache.org/docs/mod/mod_include.html

slide 49


[up]
"Mastering a Web Site" online course
Created and maintained by Lorna Schmid and David Boldt.
http://water.usgs.gov/usgs/training/webmaster/ssi.html    
Last modified: Wed Jul 18 14:00:27 EDT 2007