用apache 2的mod_ext_filter给html文件加广告。

想在静态页面上自动加个广告条,而不必手动修改每个静态页面。
按照这个需求在google上找了一段时候,不得要领。
后来想起来tomcat里有filter的概念的。
就到apache的文档里找。
瞎猫碰到了这个死耗子。
http://httpd.apache.org/docs/2.0/mod/mod_ext_filter.html
由于自己学习不认真,并且编程能力不强,笨拙的按照文档解决了这个问题。
个人感觉就是unix管道的理念。

服务器环境是centos 4.2 /apache 2.0.52

首先加载mod_ext_filter
在/etc/httpd/conf/httpd.conf
的Dynamic Shared Object (DSO) Support节
加入

Code:

LoadModule ext_filter_module modules/mod_ext_filter.so


然后定义filter的名字(advtext)和配置filter要调用程序的名字(gingeradv)。

Code:

ExtFilterDefine advtext mode=output intype=text/html cmd="/usr/bin/gingeradv"


在Directory标签里加入如下行。
Code:

SetOutputFilter advtext


加入后看起来如下
Code:

<Directory />
   Options FollowSymLinks
   AllowOverride None
   SetOutputFilter fixtext
</Directory>


用于加入广告的程序gingeradv是用perl写的,自己笨写的很垃圾。

Code:

#!/usr/bin/perl
my @lines = <STDIN>;
open ADVLINE, "/var/www/html/abc.txt" or die "cant't find the adv files";
my @advlines = <ADVLINE>;
print @lines,@advlines;


记得要给这个文件执行的权限。
Code:

chmod a+x gingeradv


要显示的广告保存在/var/www/html/abc.txt里。
我用于测试的这个是一个google的Google AdSense
abc.txt如下
Code:

<script type="text/j avascript"><!--
google_ad_client = "pub-5617089787488679";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
//--></script>
<script type="text/j avascript"
 src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


然后重新启动apache服务器

Code:

apachectl graceful


在浏览器里访问效果如下。

点击查看全图


但是这样虽然解决了问题,但perl程序好像写的很笨拙,估计没有效率。
还有这种工作方式每个请求都会调用这个perl脚本,估计访问量大的时候会不怎么好。
自己猜想写个动态加载的so才是正路,可惜不会,慢慢学习中,但愿我能学习。

引用:

本篇引用地址:
http://www.vetcafe.net/mt3/mt-tb.cgi/2424

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值