How To Enable Server Side Includes (SSI) in Apache

Last updated on June 12th, 2024 at 06:17 am

Apache allows you to configure server side includes (SSI) to add dynamic web content, html code, and reuse them more efficiently. In many cases, this is more effective than coding the entire page using dynamic programming languages such as PHP or Python. In this article, we will look at how to enable server side includes (SSI) in Apache.

What is Server Side Includes (SSI)?

Server Side Includes (SSI) are directives that are present in HTML pages but evaluated on server, when they are served. They allow you to add dynamic content to your HTML pages, without needing to code the entire page via server-side programming languages such as PHP. They are mainly used to serve dynamic content on web pages and reuse HTML codes. Here are some of the benefits of using SSI:

  1. You can use SSI to include files (other than header files) in your web pages.
  2. You can create a navigation bar/footer for your website, put it in a separate file and include it on all your web pages.
  3. SSI allows front end web developers to add dynamic elements to their site without modifying any back end code.

How To Enable Server Side Includes (SSI) in Apache

Here are the steps to enable server side includes in Apache. We will use mod_rewrite for this purpose.

1. Open .htaccess or server configuration file

Before proceeding, please enable mod_rewrite (.htaccess) in your Apache web server.

Open .htaccess file, typically located at /var/www/html/.htaccess

$ sudo vi /var/www/html/.htaccess

Alternatively, if you have access to Apache configuration file, you can open it in text editor.

$ sudo vi /etc/apache2/httpd.conf

In many cases, website administrators may not have access to main configuration file, so it is better to modify .htaccess file.

2. Enable Server Side Includes

Add the following line to .htaccess.

AddType text/html .html 
AddHandler server-parsed .html 
Options Indexes FollowSymLinks Includes

In the above code, AddType specifies the file types that are supported using server side includes. In our case, we have allowed only .html pages to have SSI. You can add more file types as per your requirement.

If you have opened server configuration file, add the following lines

Options +Includes 
AddType text/html .html 
AddOutputFilter INCLUDES .html

The above lines enable server side includes and instructs apache to look for server side includes in .html files. You can enable server side includes for other file types (e.g .shtml) also by adding following lines.

AddType text/html .shtml 
AddHandler server-parsed .shtml

3. Restart Apache web server

Restart Apache web server to apply changes.

# service httpd restart
OR 
# systemctl restart httpd
OR
# sudo service apache2 restart

4. Verify Server Side Includes

Add the following line to any of your php web page (e.g /index.php) to dynamically display today’s date on it.

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

Open browser and go to http://your_domain_or_ip/index.php. Replace your_domain_or_ip with your domain or ip address. You will see today’s date displayed at the same location where you added the above code.

Wednesday, 12-Jun-2024 19:28:54 EST

Here are some common example of server side includes:

Include File in your HTML Page

You can include another file in your HTML page using include directive. By changing the content of footer.html or quote.txt, the content of your HTML page will change automatically.

<!--#include virtual="../quote.txt" -->
<!--#include file="footer.html" -->

This directive can also be used to include header files on your web pages.

Display IP Address

The following SSI displays IP address of the user on the web page. It is often used by websites to display IP information to visitors.

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

Display Last Modified Date of Web Page

You can use the following server side include to display the last modified date & time of the given web page. This is useful to communicate the freshness of information on your website.

<!–#flastmod file=”index.html” –>

Conclusion

Apache Server Side Includes provide a simple way to include dynamic elements into your web pages without coding rendering the entire page using programming languages such as PHP or Python. This is very useful for web developers who can make changes to their site just by modifying the HTML page and not touching any of the back end code. In this article, we have learnt how to enable SSI in Apache server. The steps are the same for all versions of Apache server.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!

Also read :
How to Disable ETags in Apache
How to Fix Request URI Too Large in Apache
Apache Restrict Access to URL by IP