Recipe 1.4. Enabling Server-Side Includes


Problem

You need to display the contents of one or more shared files in the body of your web pages.

Solution

Configure your web server to parse include tags for all files, or rename your files using the server-side include (SSI) friendly suffix for files that will be parsed.

A typical web server installation will have the module for parsing SSI tags enabled by default. If you have the ability to open and modify your Apache configuration file, check to make sure the following two lines are not commented out.

The location of Apache's configuration filehttpd.confis set at installation. The default location is /etc/httpd/conf/httpd.conf. A commented, or inactive, line in the configuration file is preceded by a pound sign (#).


The two lines you're looking for should be near the top of the file:

 LoadModule includes_module libexec/httpd/mod_include.so 

and:

 AddModule mod_include.c 

Any change you make to the file will require a web server restart to take effect (see Recipe 1.9). A file's suffix determines if it is eligible for parsing. Typically, files ending with .shtml are parsed for includes, but files ending with the more familiar .html will not. Since most web page editors create files ending in .htmland most visitors to your site will assume that pages on your site end with .htmlit's a good idea to stick with that naming convention and enable SSIs on .html files, too.

Now, go back to the Apache configuration file, where you should find two lines together like this:

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

On that second line you want to add ".html" so it reads like this:

 AddHandler server-parsed .shtml .html 

If you don't have access to the master configuration file, you can still change the way Apache parses the files for your site with an .htaccess file. Just create the file in your web site root directory and paste in the first and third lines of code above, like this:

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

A web server restart is not required when you use this method.


Discussion

Server-side includes are one of the most powerful, yet easy to use, tools in a web designer's bag of tricks. Before the days of reliable web page templates built in a WYSIWYG web page editor or a content management system, SSIs were just about the only way to ensure web site consistency across a multipage site. Server-side includes allow a web designer to save shared content in a single file and display it on multiple pages.

If you're building even a modest-size web site, make sure that SSI functionality is available for every page on your web site.


Server-side includes have other magical powers, like displaying the date of a web page's last modification and executing and displaying the results of a CGI script in an otherwise static web page. We'll come back to these techniques in Chapter 4.

Bear in mind that parsing every .html file on your site for includes puts an extra load on your web server. You should not notice a decrease in your web site's performance if you follow a couple of guidelines about using SSIs. First, keep the number of SSI files included on your pages to a minimum. If you have two or three includes strung together in your page code, combine them into one file if possible. Don't build your pages out of includes.

Also, don't nest include files inside other include files. Since Apache parses files for includes based on the file suffix, give your include fileseven though they might contain HTML codea distinct suffix such as .inc or .ssi so Apache won't look through the include file for more includes to parse.



Web Site Cookbook.
Web Site Cookbook: Solutions & Examples for Building and Administering Your Web Site (Cookbooks (OReilly))
ISBN: 0596101090
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Doug Addison

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net