开启SSI配置使shtml支持include公用的页头页脚

编写编写项目众多静态文件时,能像php等开发语言一样使用include将页面公有的header/footer/sidebar作为公用、独立、单一的文件引入到各页面上,这样修改这些页面公用部分时就能单独修改一次对应的独立文件,就能全站更新,做到一劳永逸!

什么是shtml?

使用SSI(Server Side Include)的html文件扩展名,(Server Side Include),通常称为"服务器端嵌入"或者叫"服务器端包含",是一种基于服务器的网页制作技术,其文件默认扩展名是 .stm、.shtm 和 .

是一种基于服务端的网页制作技术,大多数(尤其是基于Unix平台)的web服务器如Netscape Enterprise Server等均支持SSI命令。

它的工作原因是:在页面内容发送到客户端之前,使用SSI指令将文本、图片或代码信息包含到网页中。对于在多个文件中重复出现内容,使用SSI是一种简便的方法,将内容存入一个包含文件中即可,不必将其输入所有文件。通过一个非常简单的语句即可调用包含文件,此语句指示 Web 服务器将内容插入适当网页。而且,使用包含文件时,对内容的所有更改只需在一个地方就能完成。

如何使你的Apache服务器支持SSI

Apache默认是不开启支持SSI的,需要我们更改httpd.conf来进行配置。我这里以windows平台的Apache 为例,打开conf目录下的httpd.conf文件,搜索“AddType text/html .shtml”,找到:

#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml

把这两行前面的#去掉 ,就能在后缀名为shtml的页面中使用ssi的include语法,如果想在html/htm页面中也能使用ssi的include功能,我们需要添加文件后缀名使ssi支持,修改如下:

AddType text/html .shtml .html .htm
AddOutputFilter INCLUDES .shtml .html .htm

我们会看到这两行的上面会有一句注释提示:

# (You will also need to add "Includes" to the "Options" directive.)

意思就是说除了修改这2行,还需要在你的服务器项目配目录配置的Options项中添加Includes,才能正常使用SSI的include功能。

举个例子:

我在apache的httpd-vhosts.conf文件添加了下面的虚拟主机:

<VirtualHost *:80>
ServerName www.debug.org
DocumentRoot "D:/Project"
DirectoryIndex index.html index.php index.shtml
ErrorLog "logs/eims-error.log"
CustomLog "logs/eims-access.log" combined
<Directory "D:/Project">
Options Indexes FollowSymLinks
AllowOverride all
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>

该主机域名:www.debug.org,对应虚拟目录是:D:/Project,那我们就应该在该虚拟主机中定位到Options Indexes FollowSymLinks 并在该项最后添加关键字:Includes,即修改后为:

<VirtualHost *:80>
ServerName www.debug.org
DocumentRoot "D:/Project"
DirectoryIndex index.html index.php index.shtml
ErrorLog "logs/eims-error.log"
CustomLog "logs/eims-access.log" combined
<Directory "D:/Project">
Options Indexes FollowSymLinks Includes
AllowOverride all
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>

保存httpd.conf,重起apache即可,到此我们就完成了对Apache SSI的设置。

完成SSI配置,使apache能解析SSI的shtml页面的include语法,下面就来说下页面中怎么使用include命令。

比如我们要在根目录的index.shtml引用根目录下的include目录下的header.html页面,我们可以这样写:

引用相对当前虚拟目录的header文件:

<!--#include virtual="includes/header.html" -->

<!--#include virtual="/includes/header.html" -->

路径要写正确,路径不正确会报错:[an error occurred while processing this directive]

我们还可以用include的file属性来引用文件,给出当前目录的相对路径其中不能使用"../",也不能使用绝对路径。例如: <!--#include file="header.html" -->   这就要求每一个目录中都包含一个header.html文件。(我的理解就是被包含的文件与该文件同级或者下一级目录或下下..级目录下的文件,如同级的test.html,或者同级目录下的test/test1.html)

二.如何在nginx上配置SSI
需要的选项主要是以下三个:
ssi: 默认值off,启用ssi时将其设为on
ssi_silent_errors: 默认值off,开启后在处理SSI文件出错时不输出错误提示"[an error occurred while processing the directive]"。
ssi_types: 默认是text/html,所以如果需支持html,则不需要设置这句,如果需要支持shtml则需要设置:ssi_types text/shtml
三个参数可以放在http, server或location作用域下。

三. 实例

    1. server {  
    2.     listen  10.3.9.27:80;  
    3.     server_name  www.ball.com;  
    4.     location / {  
    5.         ssi on;  
    6.         ssi_silent_errors on;  
    7.         ssi_types text/shtml;  
    8.         index index.shtml;  
    9.         root /usr/local/web/wwwroot;  
    10.         expires 30d;  
    11.         access_log      /data/logs/www.ball.com-access_log main;  
    12.     }  
    13. }  

猜你喜欢

转载自www.cnblogs.com/sjhsszl/p/8920621.html
ssi