<p>I have been experimenting with <a href="http://www.woopra.com/" rel="nofollow noreferrer">woopra.com</a> 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.</p>
<p>I attempted to work round it by using a combination of Apache rewrites and SSI's to &quot;Wrap&quot; the static html with the required code. For example...</p>
<p>I made the following changes to my apache config</p>
<pre><code>    RewriteEngine On
    RewriteCond %{REQUEST_URI} !=test.shtml
    RewriteCond %{IS_SUBREQ}  false 
    RewriteRule (.*)\.html test.shtml?$1.html
</code></pre>
<p>The test.shtml file contains...</p>
<pre><code>    &lt;script type=&quot;text/javascript&quot;&gt;
       var XXXXid = 'xxxxxxx';
    &lt;/script&gt;
    &lt;script src=&quot;http://xxxx.woopra.com/xx/xxx.js&quot;&gt;&lt;/script&gt;

    &lt;!--#set var=&quot;page&quot; value=&quot;$QUERY_STRING&quot; --&gt;
    &lt;!--#include virtual= $page --&gt;
</code></pre>
<p>The idea was that a request coming in for</p>
<pre><code>    /abc.html
</code></pre>
<p>would be redirected to</p>
<pre><code>    /test.shtml?abc.html
</code></pre>
<p>the shtml would then include the original file into the response page.</p>
<p>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.</p>
<p>Thanks</p>
<p>Peter</p>

[{'answer_id': 40156, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 3, 'selected': True, 'text': '<p>I think that <a href="http://httpd.apache.org/docs/2.2/mod/mod_ext_filter.html" rel="nofollow noreferrer">mod_filter_ext</a> 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:</p>\n\n<pre><code>while (&lt;&gt;) {\n    s/&lt;html&gt;/\\Q&lt;script&gt;....\\E/;\n    print $_;\n}\n</code></pre>\n\n<p>You could even use something like <code>sed</code> to perform the substitution.</p>\n'}, {'answer_id': 40256, 'author': 'Pablo Alsina', 'author_id': 2295, 'author_profile': 'https://Stackoverflow.com/users/2295', 'pm_score': 1, 'selected': False, 'text': '<p>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)</p>\n\n<p>This could be done a lots of way. I would suggest a small perl to to inline replacement.</p>\n'}, {'answer_id': 490116, 'author': 'Alan Doherty', 'author_id': 59995, 'author_profile': 'https://Stackoverflow.com/users/59995', 'pm_score': 0, 'selected': False, 'text': '<p>ok the method above\'s biggest problem is it would break your html validity by placing a script tag outside the <code>&lt;html&gt;</code> tags</p>\n\n<p>i\'d agree with the others on a pre-process run over your html files such as a sed/awk script</p>\n\n<p>heres a quick example {assuming the script part can be added before the <code>&lt;/head&gt;</code>\nand that the <code>&lt;/head&gt;</code> is at the start of a newline</p>\n\n<pre><code>#!/bin/bash\n\ncd /var/webserver/whatever/\n\ngrep -r \'&lt;\\/head&gt;\' */*|grep "^.*\\.html*:" &gt;/var/tmp/tempfile.txt\n((lines = $(wc -l /var/tmp/dom-tempfile.txt | awk \'{print $1}\')))\nif [ $lines -gt 0 ]\nthen\n while read line; do\n sed \'s/&lt;script type="text\\/javascript"&gt; var XXXXid = "xxxxxxx"; &lt;\\/script&gt;&lt;script src="http:\\/\\/xxxx\\.woopra\\.com\\/xx\\/xxx\\.js"&gt;&lt;\\/script&gt;&lt;\\/head&gt;/^&lt;\\/head&gt;/g\' $line&gt;/var/tmp/tempfile.htm\n mv /var/tmp/tempfile.htm $line\n done &lt; &lt;(sed \'s/\\(^.*\\.html*\\):.*$/\\1/\' /var/tmp/tempfile.txt)\nfi\nexit 0\n</code></pre>\n'}, {'answer_id': 1196832, 'author': 'Alex Lehmann', 'author_id': 27069, 'author_profile': 'https://Stackoverflow.com/users/27069', 'pm_score': 0, 'selected': False, 'text': "<p>You may have a syntax error since <code>$page</code> is not included in quotes, however the two main reasons that this doesn't are the following:</p>\n\n<ul>\n<li>include virtual should a path starting with /, in your example the query string should be /abc.html , not abc.html</li>\n<li><p>the rewrite rule should start with the path as well, so the rewrite rule has to be</p>\n\n<pre><code>RewriteRule ^(.*)\\.html /test.shtml?$1.html\n</code></pre></li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/40133', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3720/']