Apache guide: getting started with server inclusion

Source: Internet
Author: User

Server-side inclusion provides a method to add dynamic content to existing HTML documents.

This article discusses how to configure the Server to allow SSI and introduce some basic SSI technologies for adding dynamic content to existing HTML pages.

Later in this article, we will discuss how to use SSI to perform advanced tasks, such as conditional statements in SSI commands.


What is SSI?
SSI is a command on an HTML page. When the page is provided, the Server performs operations to add dynamically generated content to the existing HTML page, instead of providing the entire page through CGI programs, or using other dynamic technologies.

The trade-off between when SSI is used and when some programs are used to generate the entire page depends on how much content on the page is static and how much content needs to be re-calculated when the page is provided each time. SSI is a good way to add small pieces of information, such as the current time. If most of your pages are generated when they are provided, you need to find another solution.


Configure the server to allow SSI
To allow the server to allow SSI, the following configuration must be included in the httpd. conf file or the. htaccess file:

Options + des

Tell the server to allow parsing of SSI commands in the file. Note: In most configurations, multiple Options commands will overwrite each other. Therefore, you may have to use Options for specific directories that require SSI to ensure that they are at the end and take effect.

Not all SSI commands in files are parsed. Therefore, Apache must be told which files should be parsed. There are two types of scripts that enable apachets to contain any file with a special file suffix, such as .shtml. The configuration is as follows:

AddType text/html. shtml
AddOutputFilter between des. shtml

To execute commands with the file name suffixed with ".shtml", you may need to change the existing files added to the SSI command and all connections directed to the page.

Another method is to use the XBitHack command:

XBitHack on

XBitHack tells Apache to parse SSI commands in files with execution permissions. In this way, you only need to use chmod to make the file executable, you can add SSI commands to the existing page.

Chmod + x pagename.html

Here is a brief description: Some people will occasionally recommend that you use the. shtmlfile name without any need to use the SSI of all. html files in apache.pdf. Those may have never heard of XBitHack. You know, this will make Apache Read this file before sending the file to the client. Even if there is no SSI command in it, it will not affect the speed, so this is not a good way.

Of course, in Windows, you cannot set the corresponding execution permissions, but you should choose carefully.

By default, Apache does not send HTTP headers with the last modification date or content length for the SSI page, because these values are difficult to calculate for dynamic pages. This will prevent the page from being buffered, resulting in a noticeable reduction in client performance. There are two solutions:

Use XBitHack Full configuration. It tells Apache to determine the last modification date, only view the date of the requested file itself, and ignore the modification date of any files contained in it.
Use the commands provided by mod_expires to set a clear expiration time for the file and tell the browser and proxy that the file can be buffered.

Basic SSI commands
The SSI command has the following syntax:

<! -- # Element attribute = value... -->

The format is very similar to HTML annotations. Therefore, if SSI is not correctly configured, it will be ignored by the browser, but it will still be visible in HTML code. If SSI is correctly configured, this command is replaced by the result.

There can be many elements, and we will discuss most of them in the document of the next version. Here, we only give a few examples of SSI.

Today's date
<! -- # Echo var = "DATE_LOCAL" -->

The echo element is only the value of a variable. There are many standard variables, including all the environment variables that are valid for CGI programs. In addition, you can use the set element to define your dedicated variables.

If you do not like the date printing format, you can use the timefmt attribute of the config element to change the format.

<! -- # Config timefmt = "% A % B % d, % Y" -->
Today is <! -- # Echo var = "DATE_LOCAL" -->

File modification date
This document last modified <! -- # Flastmod file = "index.html" -->

This element is also configured in the format of timefmt.

Contains the output of a CGI program
This is a common purpose of SSI-including the output of a CGI program, such as everyone's favorite ''click counter.

<! -- # Include virtual = "/cgi-bin/counter. pl" -->


Additional example
The following are some special examples of using SSI for HTML documents.

When was the document modified?
Previously, we mentioned how to use SSI to notify users when the document was modified, but the specific implementation method is basically a problem. The following code will generate a timestamp in the HTML document. Of course, you must first make the SSI valid according to the above method.

<! -- # Config timefmt = "% A % B % d, % Y" -->
This file last modified <! -- # Flastmod file = "ssi.shtml" -->

Finally, you should replace ssi.shtml with the file name you just referenced. Therefore, if you want to simply paste a common code in any file to achieve this purpose, this method is not convenient, so the LAST_MODIFIED variable will be used:

<! -- # Config timefmt = "% D" -->
This file last modified <! -- # Echo var = "LAST_MODIFIED" -->

For details about the timefmt format, go to the search site to find strftime. The syntax is the same.

Contains a standard injection script
If you manage a site with many pages, you will find it very painful to make changes to all pages, especially when trying to maintain a standard view of all pages.

Using a method that contains a header and/or footer can reduce the modification burden. You only need to create a script file and use the includeSSI command to include it to every page. The include element can determine the file to be included based on the file attribute or virtual attribute. The file attribute is a file path relative to the current directory, that is, it cannot be an absolute file path (starting ). The virtual attribute may be more useful. It is a URL relative to the provided document and can start with a slash (/), but must be on the same server as the provided document.

<! -- # Include virtual = "/footer.html" -->

The combination of SSI commands and Script Injection files is very useful. For example, the LAST_MODIFIED command is used in Script Injection files. SSI commands can appear in include files, while include commands can be nested, that is, one include file can contain another.


Other settings
In addition to setting the time format, config also has two purposes.

When an SSI command error occurs, the following message is generated:

[An error occurred while processing this directive]

To change the message format, you can use the errmsg attribute of the config element:

<! -- # Config errmsg = "[It appears that you dont know how to use SSI]" -->

Of course, the end user will never see this message, because you have solved these problems before the website is put into operation (right ?).

You can also use the sizefmt attribute of config to set the format of the returned file size, in bytes, or in the unit of Kb or Mb.


Execute Command
I hope to write another article about using SSI for small CGI programs in the next few months. Here, I will only introduce the use of exec. SSI can indeed execute commands using shell (/bin/sh, precisely, or DOS shell in Win32. The following example generates a directory list:

<Pre>
<! -- # Exec cmd = "ls" -->
</Pre>

In Windows:

<Pre>
<! -- # Exec cmd = "dir" -->
</Pre>

You may find that the result of this command in Windows is somewhat strange. The output of dir contains the string ''<dir>, which may confuse the browser.

Note that this feature is extremely dangerous because it will execute any commands contained in the exec tag. If the user may modify the content of your webpage, such as the message book, you must disable this function. Add the IncludesNOEXEC parameter to the Options command to disable the exec function and retain SSI.


Advanced SSI Technology
In addition to separating content, Apache SSI can also set variables for comparison and conditional expressions.

Warning
Most of the features discussed in this article are only valid in Apache 1.2 and later versions. If you are not running Apache 1.2 or the latest version, please upgrade it immediately or as soon as possible. Now, we will continue to talk about it after you have finished it.

Set Variables
You can use the set command to set variables for future use. The syntax is:

<! -- # Set var = "name" value = "Rich" -->

In addition to setting text variables, you can also set any other variables, such as environment variables and some previously mentioned variables (such as LAST_MODIFIED), as your dedicated variables. Prefix the variable name with $, indicating that it is a variable rather than a literal string.

<! -- # Set var = "modified" value = "$ LAST_MODIFIED" -->

Use $ in a string. escape characters must be used.

<! -- # Set var = "cost" value = "$100" -->

Finally, if you want to enclose the variable name in a long string, you can enclose it in curly brackets, to avoid confusion between variable names and other characters (it is difficult to illustrate this situation, but you still want to understand it ).

<! -- # Set var = "date" value = "$ {DATE_LOCAL }_$ {DATE_GMT}" -->

Conditional expressions
With variables, you can set and compare their values to indicate conditions. SSI has become a simple programming language. Mod_include provides structures such as if, elif, else, And endif to construct conditional statements, so as to efficiently generate multiple logical pages for a real page.

The condition structure is as follows:

<! -- # If expr = "test_condition" -->
<! -- # Elif expr = "test_condition" -->
<! -- # Else -->
<! -- # Endif -->

Test_condition can be any logical comparison-one value can be compared with another value, you can also test whether a specific value is "true" (a given string is true if it is not null ). For a complete list of comparison operators, see mod_include. The following are examples that may be used.

In the configuration file, you can set it as follows:

BrowserMatchNoCase macintosh Mac
BrowserMatchNoCase MSIE InternetExplorer

If the client runs Internet Explorer on the Macintosh, set the environment variables ''mac and ''internetexplorer to true in the preceding example.

Then, in the document that allows SSI, you can set it as follows:

<! -- # If expr = "$ {Ma

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.