【发布时间】:2015-10-25 22:22:22
【问题描述】:
我不知道 Apache 或 .htaccess 是否是正确的方法,但我想知道如何将 HTML 代码插入到 public_html 目录内的所有页面,有点像 2000 年代早期的免费主机在所有页面中插入他们的横幅。
注意:我不是在说手动编辑每个页面并添加SSI或PHP的include()
【问题讨论】:
我不知道 Apache 或 .htaccess 是否是正确的方法,但我想知道如何将 HTML 代码插入到 public_html 目录内的所有页面,有点像 2000 年代早期的免费主机在所有页面中插入他们的横幅。
注意:我不是在说手动编辑每个页面并添加SSI或PHP的include()
【问题讨论】:
您可以使用以下方法将 HTML 代码插入到使用 .htaccess 的特定目录中的所有页面中:
<Directory "/public_html/">
# Prepend to top
php_value auto_prepend_file "/dir/path/banner.php"
# Append to bottom
php_value auto_append_file "/dir/path/footer.php"
</Directory>
以下文章讨论了 .htaccess 如何允许您将 html 附加到每个页面请求:
http://davidwalsh.name/prepend-append-files-htaccess
以下文章讨论如何使用 .htaccess 目录块:
【讨论】:
您可以通过使用 ExtFilterDefine 和 SetOutputFilter 创建自定义输出过滤器并运行 PHP 脚本来执行此操作,该脚本读取 stdin:// 然后插入您想要的代码并输出它。查看http://httpd.apache.org/docs/2.2/mod/mod_ext_filter.html 以开始使用。
如果您只关心 PHP 输出而不关心静态 HTML 文件,您可以使用 auto_prepend_file 运行调用 ob_start() 的脚本,并使用 auto_append_file 运行调用 $html = ob_get_clean() 的脚本并将代码插入$html 在回显之前。
【讨论】:
我可以通过 Apache 将 html 插入所有页面,但仅限于由我的 apache 托管的站点,而不是通过我的 apache 代理服务器托管的所有站点。
这就是它的工作原理。 我设置了一个 XAMPP,为 apache 2.4 下载 mod_layout 5.1。 (安装 mod_layout 很简单,如果你使用 XAMPP for windows,只需下载 mod_layout.so 5.1 for windows 并将其放入你的 apache 模块文件夹 $home/apache/module,然后将配置 DSO 添加到你的 httpd.conf LoadModule layout_module modules/ mod_layout.so - 如果你使用linux或其他操作系统,你应该下载mod_layout.so for linux,然后运行make命令安装)
为 windows 安装 mod_layout.so 后,只需将此代码放在您的 .htaccess 文件或 httpd.conf 中,您将获得 html 插入到您的所有页面:
<IfModule mod_layout.c>
AddOutputFilter LAYOUT html
AddOutputFilter LAYOUT htm
AddOutputFilter LAYOUT shtml
AddOutputFilter LAYOUT shtm
AddOutputFilter LAYOUT cgi
AddOutputFilter LAYOUT php
LayoutFooter "C:/xampp/apache/cgi-bin/footer.php"
</IfModule>
C:/xampp/apache/cgi-bin/footer.php 是您放置 html 或 php 文件的位置,在我的情况下是 C:/xampp/apache/cgi-bin/footer.php,但您可以放置在任何地方,只要给正确的路径,你会没事的
【讨论】: