Using Server-Side Includes
Server-side includes (SSIs) allow authors to include some dynamic information in static HTML pages. For instance, a standard header or footer can be included in all documents in a web site without the need to reproduce the HTML code on each page. Or, information about the document, such as the last time it was updated, can be displayed automatically.
SSI commands are embedded in HTML files, enclosed in HTML comment tags so that they will not be displayed. When a browser requests a file containing SSI commands, the server parses (reads) and executes the commands before sending the page contents to the browser. In this way, the contents of an external file can be included on a page, or real-time information, such as the system date, can be displayed.
Enabling Server-Side Includes
By default, the web server does not parse a file before serving it to the browser. In order to get the server to search for SSIs before displaying a page:
- place a file called .htaccess in the directory which includes the directive:
Since server parsing takes extra time, it's not wise to use this method unless a majority of the .html files in the directory require parsing.
AddHandler server-parsed .html
If the files you want parsed have a different file extension, first define that extension as text/html:
AddType text/html .ssi
AddHandler server-parsed .ssi
SSI commands are embedded in HTML code using HTML comments, e.g.,
in the location where the command results should be displayed.
Prints the size of a specified file.
Example: The size of this file is
Displays: The size of this file is .
Prints the last modification date of the specified file.
Example: Last modified on
Displays: Last modified on .
Inserts the text of another document or file into the parsed file.
Specifying File Paths
Use the "virtual" attribute to specify a file path in a command.
The virtual path is interpreted in the same way as a partial URL would be: If it begins with a slash (/), the path is taken to begin at the server root. If it does not begin with a slash (/) then it is taken to be relative to the current document.
Include variables provide information on the current document, server, browser and date. The most commonly-used variables are:
- The browser client in use
- The browser's IP address
- The local date and time
- When the document was last updated
- The document's URL
You can display the value of a variable using the echo command:
Prints the value of an include variable.
Example: Last modified on
Displays: Last modified on
(a better way to display the last modification time of the current file)
You can use the SSI printenv command to see a list of all variables and their current values:
Prints a list of all variables and their attributes