让 Apache 动态修改静态网页
我一直在尝试 woopra.com 一个网络分析工具。 这需要在每个页面添加一段 JavaScript 代码才能运行。 对于具有通用页眉或页脚的动态站点来说这很容易,但对于完全静态的 html 页面则不然。
我尝试通过使用 Apache 重写和 SSI 的组合来解决这个问题,用所需的代码“包装”静态 html。 例如...
我对我的 apache 配置进行了以下更改
RewriteEngine On
RewriteCond %{REQUEST_URI} !=test.shtml
RewriteCond %{IS_SUBREQ} false
RewriteRule (.*)\.html test.shtml?$1.html
test.shtml 文件包含...
<script type="text/javascript">
var XXXXid = 'xxxxxxx';
</script>
<script src="http://xxxx.woopra.com/xx/xxx.js"></script>
<!--#set var="page" value="$QUERY_STRING" -->
<!--#include virtual= $page -->
这个想法是,传入的请求
/abc.html
将被重定向到
/test.shtml?abc.html
shtml,然后将原始文件包含到响应页面中。
不幸的是,它并没有按计划工作:)任何人都可以看到我做错了什么,或者也许建议一种替代方法。 有没有 apache 模块可以做同样的事情。 优选地,可以基于每个站点进行配置。
谢谢
彼得
I have been experimenting with woopra.com A web analytics tool. Which requires a piece of javascript code to be added to each page to function. This is easy enough with more dynamic sites with universal headers or footers but not for totally static html pages.
I attempted to work round it by using a combination of Apache rewrites and SSI's to "Wrap" the static html with the required code. For example...
I made the following changes to my apache config
RewriteEngine On
RewriteCond %{REQUEST_URI} !=test.shtml
RewriteCond %{IS_SUBREQ} false
RewriteRule (.*)\.html test.shtml?$1.html
The test.shtml file contains...
<script type="text/javascript">
var XXXXid = 'xxxxxxx';
</script>
<script src="http://xxxx.woopra.com/xx/xxx.js"></script>
<!--#set var="page" value="$QUERY_STRING" -->
<!--#include virtual= $page -->
The idea was that a request coming in for
/abc.html
would be redirected to
/test.shtml?abc.html
the shtml would then include the original file into the response page.
Unfortunately it doesn't quite work as planed :) can anyone see what I am doing wrong or perhaps suggest an alternative approach. Is there any apache modules that could do the same thing. Preferably that can be configured on a per site basis.
Thanks
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会遇到语法错误,因为
$page
未包含在引号中,但是不包含在引号中的两个主要原因如下:重写规则也应该以路径开头,因此重写规则必须是
You may have a syntax error since
$page
is not included in quotes, however the two main reasons that this doesn't are the following:the rewrite rule should start with the path as well, so the rewrite rule has to be
好吧,上面的方法最大的问题是,它会通过在
标签之外放置一个脚本标签来破坏你的 html 有效性,
我同意其他人对你的 html 文件进行预处理运行的意见,例如作为 sed/awk 脚本,
这里有一个快速示例{假设脚本部分可以添加在
之前
并且
位于换行符的开头
ok the method above's biggest problem is it would break your html validity by placing a script tag outside the
<html>
tagsi'd agree with the others on a pre-process run over your html files such as a sed/awk script
heres a quick example {assuming the script part can be added before the
</head>
and that the
</head>
is at the start of a newline如果页面是静态的,为什么要动态更改它们,而不是预处理站点上的所有页面,向每个页面添加所需的 javascript 片段? 这很简单,而且可能更有效(您的浏览量可能比要更改的页面还要多)
这可以通过多种方式完成。 我建议使用一个小的 perl 来进行内联替换。
If the pages are static, why would you change them on the fly instead of preprocessing all pages on a site, adding the piece of requiered javascript to each one of them? This is simple and probably more efficent (you probably have more pageviews than pages to change)
This could be done a lots of way. I would suggest a small perl to to inline replacement.
我认为 mod_filter_ext 是您正在寻找的模块。 您可以编写一个简短的 Perl 脚本,例如在页面中插入 JS 代码并将其注册以处理 HTML 页面:
您甚至可以使用诸如
sed
之类的东西来执行替换。I think that mod_filter_ext is the module you are looking for. You can write a short Perl script for example to insert the JS code in the pages and register it to process HTML pages:
You could even use something like
sed
to perform the substitution.