Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Apache to modify static webpages on the fly

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

like image 303
Vagnerr Avatar asked Dec 14 '25 21:12

Vagnerr


1 Answers

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:

while (<>) {
    s/<html>/\Q<script>....\E/;
    print $_;
}

You could even use something like sed to perform the substitution.

like image 142
Grey Panther Avatar answered Dec 17 '25 21:12

Grey Panther



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!