Home arrow Apache arrow Page 2 - One Way To Use Server Side Includes

One Way To Use Server Side Includes

Heard the acronym SSI but no sure what they are? Join Corbb in this article as he explains SSI and also shows us some interesting examples on how to use them with our website.

Author Info:
By: Corbb O'Connor
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
August 08, 2002
  1. · One Way To Use Server Side Includes
  2. · What In The World Are Server Side Includes?
  3. · Conclusion

print this article

One Way To Use Server Side Includes - What In The World Are Server Side Includes?
(Page 2 of 3 )

Often referred to by its acronym SSI, server side includes are a utility found on almost all web servers. Here's how it works:
  1. The browser tells the server that it wants a certain page
  2. The server notices that it needs to parse the page for SSI commands
  3. The server parses the commands
  4. The parsed page is sent to the browser
Configuring Your Server
Now that you know what SSI is, we need to tell the server which files to parse for SSI code. In order to do this, you need to have access to a file called .htaccess. If you have access to this file, skip the rest of this paragraph and paste the code below into a new line within that file. On the other hand, if you do not know how to access this file, contact your web host. Unfortunately, some web hosting companies do not allow access to the .htaccess file. If you are unable to access the file, ask them to add the following line of code to the file. If you still cannot get them to update your .htaccess file, try using the code in this article. It could be that your website is already configured for SSI.

addType text/x-server-parsed-html .shtml

So what does this mean? The addType directive lets the server know that you are changing the way it handles a certain file extension. In this case, you are telling the server to look for SSI tags in every page with a .shtml extension. Without the above code, the server will not parse files for SSI code. It will leave the commands as they are, and send them to the browser. Most browsers will ignore the tags because they appear as comments (i.e. <!--#include virtual="page2.htm" -->)

On With The Code Already!
Now that the server is setup, it is time to start creating our site with SSI. Take the following code and place it in a file called display.html:

<!-- display.shtml -->
<!--#set var="which" value="$QUERY_STRING_UNESCAPED" -->
<!--#if expr="$which != ''" -->
<!--#include virtual="content/$which" -->
<!--#else -->
<!--#include virtual="content/error.txt" -->
<!--#endif -->

To make this system work, we need to make a few adjustments for your server:
  • The text in red should be the path to the folder that will hold the content files. You can also make this your home directory (in which case just use a "/" without the quotes).
  • The text in blue should be the path to the error.txt file. It is best to place this file in the same directory as the text in red. Make sure that you keep the text that says "error.txt" intact.
Now minimize that file for a moment. Create a new folder on your computer and on your website called "content". You can call the folder whatever you like, just make sure that the title and path match the path you entered in the display.html file.

Create a new file, and call it file1.txt. Put the following code in it:

<title>MySite.com – SSI Is So Cool!</title>
<p><font size="3" ><b>Lookie here! Some content!</b></font></p>
<p><font size="2"><I>I can use any HTML code I want!</I></font></p>

Finally, make a file called error.txt. The location of this file should be the same as the blue text in the display.html file. Copy the following code into the file:

<title>MySite.com – Error</title>
<P ALIGN="CENTER"><FONT SIZE="4"><B>Error!</B></FONT></P>
<P><FONT SIZE="2">The file that you are looking for cannot be found. If you followed a link from within this website, please email <A HREF="mailto:john@doe.com">John Doe</A>. Thank you!</FONT></P>

Feel free to change the contents of this file. If you do wish to keep it the same, make sure that you change the email address to your own.

How Do I Test The SSI?
To access the pages on your website, use the following format for the web address:


Of course, you need to change the beginning part to reflect your website address and file1.txt with the file that you are trying to display.

You can also use hyperlinks the same way as if you were linking to a regular page:

<a href="display.shtml?file1.txt" >Click here!</a>
blog comments powered by Disqus

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials