Enabling server-side includes (SSIs) in Apache
Server-side includes are the recommended way of including one html file in another. This is very convenient if you have a file that changes frequently. Or it would be, except for one problem: the included file cannot have an html header or <body> statements, or the page will not validate. This means your included file cannot be a complete html page, but must be an HTML fragment.
- Compile apache, specifying
configurecommand (see linuxsetup42.html).
- Type the line
./httpd -l | grep mod_include. It should print
mod_include.c, which indicates that mod_include is compiled into apache.
- If your version of apache was already compiled by someone else, verify that
it contains the compiled-in mod_include option by typing
httpd -l. If it doesn't, you will have to find a file called
mod_include.sosomewhere (on my system, it's at /usr/lib64/apache2-prefork/mod_include.so). Apache2 must load this file as a separate module. To do this, add the line
LoadModule include_module modules/mod_include.soto your httpd.conf file.
- Add the following lines to your httpd.conf file:
AddType text/html .shtml AddHandler server-parsed .shtml AddType text/html .html AddHandler server-parsed .htmlThe first two lines let you use includes in files with the extension .shtml. The last two lines let you use includes in files with the extension .html. Many people use only the first two, because most files don't contain includes. This reduces the load on the server somewhat; however, in that case, SSIs will only work in files with an .shtml extension. You will then have to deal with complaints from people who can't find your page because they forgot to type the 's'.
Includesto your Options section in httpd.conf like so:
<Directory "/usr/local/apache2/htdocs"> Options FollowSymLinks ExecCGI Includes ... other stuff ... </Directory>
- Restart apache
./apache2 stop ./apache2 start
Note: simply typing
./apache2 restartdoesn't always work.
- Create an html or shtml file as appropriate, and add some include lines
in the body:
<!--#include virtual="some-included-file.html" --> <!--#include file="some-included-file.html" --> <!--#echo var="DATE_LOCAL" --> This file last modified <!--#echo var="LAST_MODIFIED" -->
Other variables are DATE_GMT, DOCUMENT_URI, DOCUMENT_PATH_INFO, USER_NAME, DOCUMENT_NAME, and QUERY_STRING_UNESCAPED.
- Note that, because of where the Options section was placed in the httpd.conf file, SSIs will only work if the file is in the htdocs file. If you open the file somewhere else (for example, using file:///) it won't work.
- If SSIs are working on this server, you should see a date below
before the word "today."
This file last modified today.