Apache替换页面输出内容

使用Apache的 mod_ext_filter 组件可以对返回页面的内容进行过滤,通过该组件,可以调用一个外部程序来实现filter功能。
mod_ext_filter Documentation: http://httpd.apache.org/docs/2.2/mod/mod_ext_filter.html

首页找到 /etc/httpd/conf/httpd.conf 文件,查看模块是否启用:

LoadModule ext_filter_module modules/mod_ext_filter.so

然后到 /etc/httpd/conf.d/ 新建配置文件 mod_ext_filter.conf

<IfModule mod_ext_filter.c>
  # mod_ext_filter directive to define a filter which
  # replaces text in the response
  #
  # 将 [ Hello-=:"World"! ]  替换为 [ Hello, "Leon" ]
  ExtFilterDefine fixtext1 mode=output intype=text/html cmd="/bin/sed s/Hello-=\:\"World\"!/Hello,\ \"Leon\"/g"
  # 将 [ <a href="http://www.baidu.com/">Baidu</a> ]  替换为 空
  ExtFilterDefine fixtext2 mode=output intype=text/html cmd="/bin/sed 's,<a href=\"http://www.baidu.com/\">Baidu</a>,,g'"

  <Location />
    # core directive to cause the fixtext filter to
    # be run on output
    SetOutputFilter fixtext1;fixtext2
  </Location>
</IfModule>