As a follow-up to our class discussion, here’s a summary regarding Server-Side Includes (SSI) on our class server, lolin02.
The whole reason for using SSI is that there are frequently many lines of code in a website which are repeated on every page. Rather than retyping (or copy-and-pasting) the code for each page, we put it into a separate file and let the server plug it in when serving up the page. This has the advantage that if we need to make any changes to the code, we only have to make the change in one place and it will be automatically reflected on every page. Section 3.12 of the textbook does a decent job of explaining all this.
A couple key points to keep in mind
- For any file which contains SSI directives, you need to let the server know that it needs to scan that file for the directives. There are two approaches, described below. The files that are being included do not need any special handling.
- When the server receives a request for a page that has SSI directives, the server will send to the client a single page which includes all the code from the various included pages. The client receives a single page that has everything in it — the end result is no different from what you’d get if you had created a single large file to begin with.
- The files being included are not complete web pages. Make sure that the complete served page contains only one <!DOCTYPE>, one <html></html>, one <head></head>, and one <body></body>. (Your code will not validate otherwise.)
Okay, are you still with me? Now let’s talk about how you tell the server that a page needs to be scanned for SSI directives. This is where things get just a little messy, because it depends on the web server software. These notes apply specifically to lolin02, and generally to any server running Linux and Apache Web Server. (And this information is also in Blackboard, under Course Documents | Slides and Notes.)
Two approaches are available. You can use either one — the end result is the same. I recommend that you use one approach consistently.
- Instead of naming your file something.html, name it something.shtml. That’s all there is to it! (But don’t forget that any other pages that link to this page must specify the correct filename, that is, with the .shtml extension.) Note also that the files being included do not need the special extension. Also note that you shouldn’t use .shtml unless it is specifically needed — otherwise, you’re making the server do a lot of extra work, which can slow down the responsiveness of the web site.
- The other possibility is to “make the file executable”. If you already understand Linux file permissions (or even Windows file permissions), it’s just a matter of adding execute permission to the file. You can log into lolin02 using an ssh client, using the same student*** username and fruity password as you use with ftp, and from there make the necessary change. See the next blog post (or Blackboard) for more details!