Server Side Includes

Installation (Apache)

  1. Uncomment module in httpd.conf: LoadModule include_module modules/mod_include.so mod_include
  2. Configure module in httpd.conf or .htaccess: # Enable SSI Options +Includes # Parse all .shtml files ... #AddType text/html .shtml #AddOutputFilter INCLUDES .shtml # ... or parse all.html files (bad performance!) ... #AddOutputFilter INCLUDES .html # ... or parse all files with the execute bit set (unix only) XBitHack on
  3. For XBitHack, change the execute bit: chmod +x file.html

Basic Commands

Whitespace is important!

<!--#include file="ssi-file.html" -->
Replaces the comments with the content of ssi-file.html. Path must be relative to the current file. Cannot contain ../
<!--#include virtual="ssi-file.html" -->
URL-path. Either absolute to the root (begins with /) or relative to the current file. Can be used to point to cgi
<!--#include virtual="/a.html" onerror="/error.html" -->
Show the content of error.html if the include doesn't work
<pre><!--#printenv --></pre>
Show all available variables

Legacy expression syntax

Used before version 2.3.13. To active it in newer versions add SSILegacyExprParser on to .htaccess or httpd.conf


$variable
value of variable
string
true if string is not empty. Everything that's not a variable or operator is treated as a string. Strings that contain whitespace must be quoted: 'string'
-A string
true if the URL represented by the string is accessible. Useful to show content/links only to authorized users.
string1 = string2 or string1 == string2
true if the two strings are equal
string = /regex/ or string == /regex/
true if the string matches the regular expression. Groups are stored in the variables $0 .. $9 and can be accessed later in the code
string1 != string2 or string != /regex/
true if the two strings are not equal / string doesn't match the regular expression
str1 < str2 or str1 <= str2 or str1 > str2 or str1 >= str2
Compare string1 with string2. Strings are compared literally (using strcmp(3)). Therefore the string "100" is less than "20"
( condition )
Can be used to clarify the order of operations
! condition
Negates the condition
test_condition1 && test_condition2
true if both test_condition1 and test_condition2 are true
test_condition1 || test_condition2
true if either test_condition1 or test_condition2 is true

Flow Control

<!--#if expr="v('DOCUMENT_NAME') == 'index.html'" --> <h1>Start here</h1> <!--#elif expr="v('DOCUMENT_NAME') == 'contact.html'" --> <h1>Please contact us!</h1> <!--#else --> <h1>Unknown site</h1> <!--#endif -->

The condition follows the ap_expr or the legacy expression syntax (see below). Use <!--#printenv --> to see all available variables.

More Information

Other Commands