Getting More Out Of Apache (Part 1) - Including A Few More Tricks
(Page 5 of 5 )
The next item on today's agenda involves something called "server-side includes", or SSI.
Very simply, server-side includes are sets of instructions which enable Web servers to display data contained in server-side variables (server date and time, file currently in use, and so on). SSI also allows you to include external text files in your HTML pages - this comes in handy when you need to add a common header and footer to every HTML page.
Adding support for server-side includes is again a matter of tweaking the Apache configuration file, "httpd.conf". By default, the server root is not configured to parse documents containing SSI instructions. In order to enable this support, you need to add the
Includes
keyword to the <Directory> directive - it should look something like
this:
Options Indexes FollowSymLinks Includes
Next, you need to tell the server to recognize SSI files - these typically
have the file extension ".shtml". In order to do this, add the following lines to the configuration file:
# specify MIME type for SSI files
AddType text/html .shtml
AddHandler server-parsed .shtml
Restart Apache for the changes to take effect.{mospagebreak title=Hello,
World!} With that done, it's time to take see how server-parsed HTML files work. Create the following simple file, and save it as "hello.shtml".
<html>
<head>
</head>
<body>
You have just loaded <!--#echo var="DOCUMENT_NAME" -->
</body>
</html>
Now, when you point your browser to this page (via the Web server), you
should see something like this
You have just loaded hello.shtml
Thus, the SSI directive allows you to obtain the name of the current
document without you - the document author - having to explicitly specify it.
SSI also allows you to display the size and last modification time of the current file - take a look!
<html>
<head>
</head>
<body>
You are currently viewing <!--#echo var="DOCUMENT_NAME" --> (<!--#fsize
file="$DOCUMENT_NAME" -->), last modified on <!--#flastmod
file="$DOCUMENT_NAME"-->
</body>
</html>
And the output would be:
You are currently viewing hello.shtml ( 1k), last modified on Thursday,04-Jan-2001 21:28:33 IST
And you can also display the current server time, as illustrated below:
<!--#config timefmt="%I:%M %p" -->
<html>
<body>
Current time is <!--#echo var="DATE_LOCAL" -->
</body>
</html>
Finally, SSI also allows you to separate common elements of your Web pages
into separate files, and include these files on each and every page. For example, if I have a copyright notice that is to be displayed on each and every page, I could place it in a file named "footer.txt"
This material copyright Melonfire, 2001. All rights reserved.
and then create HTML pages which looked like this:
<html><head></head><body>This is where proprietary material goes.<p><!--#include file="footer.txt"></body></html>
Now, each time a page like the one above is requested from the Web server, Apache will automatically include the contents of the file "footer.txt" in the appropriate place. The final page might look something like this:
This is where proprietary material goes.
This material copyright Melonfire, 2001. All rights reserved.
Be warned, however, that extensive use of SSI can affect Web server
performance, since the server has to first parse the file before sending it to the client browser - so use it judiciously.
Detailed information on SSI can be obtained from the SSI resources at
http://httpd.apache.org/docs/howto/ssi.html And that's about it for this time. Come back for the second part of this article, where I'll be discussing URL rewriting, user authentication and a few other tricks.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |