启用 Apache SSI 必须四件事缺一不可:启用 mod_include 模块、目录配置 Options +Includes、AddType 与 AddOutputFilter 成对设置 .shtml 后缀、被包含文件权限为 644 且 SELinux 策略允许;路径语义需严格区分 file(相对路径,不支持 ../ 和 / 开头)与 virtual(绝对路径,必须以 / 开头);.shtml 后缀非建议而是默认触发解析的必要条件,改用 .html 会降低性能且静态托管平台通常不支持;调试优先查 error_log,八成问题源于路径或权限错误。

Apache 里启用 SSI 必须配这四件事
不配全,<!--#include file="header.html" --> 就是纯注释,不会生效。
缺一不可:
-
mod_include模块必须启用:检查httpd -M | grep include,没输出就去httpd.conf取消LoadModule include_module modules/mod_include.so的注释 -
Options +Includes要写在对应目录的<directory></directory>块里,不能只写在全局或虚拟主机顶层 -
AddType text/html .shtml和AddOutputFilter INCLUDES .shtml必须成对出现,且后缀名要和实际文件一致 - 被包含文件(如
header.html)权限得是644,SELinux 环境下还要确认httpd_read_content是否允许
file 和 virtual 路径写错直接 500
SSI 里两种路径语义完全不同,混用必出错。
file="header.html" 是相对当前文件的路径,不能带 ../;virtual="/includes/footer.html" 是相对于网站根目录(DocumentRoot),必须以 / 开头。
立即学习“前端免费学习笔记(深入)”;
常见错误:
- 写
file="/includes/header.html"——file不接受绝对路径,Apache 直接报 500 - 写
virtual="includes/header.html"—— 缺少开头斜杠,SSI 找不到文件,静默失败 - 主页面在
/blog/post.shtml,想包含同级的nav.html,却写成virtual="/blog/nav.html"—— 多此一举,用file="nav.html"更稳
为什么必须用 .shtml 后缀
不是“建议”,是 Apache 默认行为:只有识别为 SSI 类型的文件,才会触发解析。用 .html 后缀默认跳过所有 <!--#include --> 指令。
如果硬要用 .html,就得在配置里把 AddType 和 AddOutputFilter 改成针对 .html,但后果是:所有 HTML 页面都会被强制走一遍 SSI 解析流程,哪怕没写 include,性能有损。
更麻烦的是,GitHub Pages、Netlify 等静态托管平台默认不支持 SSI,这类配置只在你可控的 Apache/Nginx 环境下有效。
调试时先看 error_log,别瞎猜
页面空白、显示原始注释、或报 an error occurred while processing this directive,别急着改 HTML,先查 Apache 错误日志。
典型线索:
-
unable to include "header.html": file permissions deny server access—— 文件权限或 SELinux 阻止读取 -
unable to include "header.html": file not found—— 路径错,或者当前目录不是你以为的那个目录 -
Content-Type not set correctly for .shtml——AddType没生效,响应头是text/plain,浏览器当文本渲染
路径和权限问题占八成,日志里通常一句话就定位到根因。



















