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:
- The browser tells the server that it wants a certain page
- The server notices that it needs to parse the page for SSI commands
- The server parses the commands
- The parsed page is sent to the browser
Configuring Your ServerNow 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 .shtmlSo 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 -->
<html>
<head>
</head>
<body>
<!--#set var="which" value="$QUERY_STRING_UNESCAPED" -->
<!--#if expr="$which != ''" -->
<!--#include virtual="content/$which" -->
<!--#else -->
<!--#include virtual="content/error.txt" -->
<!--#endif -->
</body>
</html>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:
<!—file1.txt-->
<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:
<!—error.txt-->
<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:
http://www.yourwebsite.com/display.shtml?file1.txt
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>Next: Conclusion >>
More Apache Articles
More By Corbb O'Connor