Skip to main content

Using Server-Side Includes (SSI)

1. Enabling SSI Module

Server-Side Includes (SSI) are disabled by default in Apache HTTP Server. To enable SSI, you must load the mod_include module.

Directive Values Description
LoadModule include_module modules/mod_include.so Loads the SSI module into Apache

Example: Enabling SSI Module

<IfModule mod_include.c>
    LoadModule include_module modules/mod_include.so
</IfModule>

2. Configuring SSI Processing

SSI processing can be controlled via directives within the configuration file. These directives define how and when SSI is processed.

Directive Values Description
Options Includes Enables SSI processing for the directory or file
Options IncludesNoExec Enables SSI but disables command execution

3. Setting SSI Handler

The SSI handler must be assigned to files that should be processed for SSI directives. This is typically done using the SetHandler directive.

Directive Values Description
SetHandler server-parsed Assigns the SSI handler to a file or directory

Example: Setting SSI Handler

<Files "*.shtml">
    SetHandler server-parsed
</Files>

4. Using Include Directive

The <!--#include--> directive is used to include content from other files into the current document.

Directive Values Description
include file="filename" Includes content from a file
include virtual="/path/to/file" Includes content from a virtual path

Example: Using Include Directive

<!--#include file="header.html" -->

5. Executing Commands

The <!--#exec--> directive allows execution of shell commands on the server. This feature should be used carefully due to security risks.

Directive Values Description
exec cmd="ls -l" Executes a shell command
exec virtual="/cgi-bin/script.sh" Executes a CGI script
Warning: Enabling command execution can pose significant security risks. Ensure proper permissions and validation.

6. Displaying Variables

Variables can be displayed using the <!--#echo--> directive, which outputs the value of an environment or SSI variable.

Directive Values Description
echo var="DATE_GMT" Displays the current date in GMT
echo var="SERVER_SOFTWARE" Displays the server software name

Example: Displaying Variables

<!--#echo var="DATE_GMT" -->

7. Setting SSI Variables

The <!--#set--> directive allows setting SSI variables for use in other directives.

Directive Values Description
set var="myVar" value="Hello World" Sets a new SSI variable

Example: Setting SSI Variables

<!--#set var="myVar" value="Hello World" -->

8. Using Conditional SSI

The <!--#if--> directive allows conditional execution of content based on variable values or command output.

Directive Values Description
if expr="env('REMOTE_ADDR') == '192.168.1.1'" Conditionally includes content if the remote address matches
if expr="req('user-agent') =~ /MSIE/" Conditionally includes content if the user agent is Internet Explorer

Example: Using Conditional SSI

<!--#if expr="env('REMOTE_ADDR') == '192.168.1.1'" -->
    <p>Access granted to local network</p>
<!--#endif -->

9. Using File Includes

File includes are a common use case for SSI, allowing parts of content to be reused across multiple pages.

Directive Values Description
include file="footer.html" Includes the footer content
include virtual="/shared/header.html" Includes the header content from a virtual path

Example: Using File Includes

<!--#include file="footer.html" -->

10. Configuring SSI Error Messages

Error messages can be customized to provide better feedback when SSI directives fail.

Directive Values Description
SSIError custom="<p>SSI Error</p>" Customizes the error message for SSI processing failures

11. Testing SSI Functionality

To verify that SSI is working correctly, you can create a test file and check its output.

Steps to Test SSI

  1. Create a test file with an SSI directive like <!--#echo var="DATE_GMT" -->
  2. Ensure the file has the correct handler assigned (e.g., SetHandler server-parsed)
  3. Access the file through a web browser or command-line tool like curl
  4. Verify that the SSI directive is replaced with actual content

12. Understanding SSI Security Considerations

SSI can introduce security vulnerabilities if not configured properly. It is important to understand the risks and how to mitigate them.

Security Considerations

  • Do not allow command execution unless strictly necessary
  • Validate all input to SSI directives
  • Restrict access to sensitive files that may contain SSI directives
  • Use IncludesNoExec to prevent command execution
Note: SSI should be used carefully in production environments to avoid exposing sensitive server information or allowing arbitrary command execution.

Was this article helpful?