I've got an Apache web server that delivers static HTML pages. For some reason I can't change the files themselves, but I still want to inject some HTML into every page that is being served.

Is this possible with mod_proxy? If not, could anyone recommend a software that provides such a feature?

EDIT: I have to insert some kind of banner ad (e.g. a javascript) and a tracking pixel.

link|improve this question
feedback

7 Answers

up vote 8 down vote accepted

You could do this: Work with mod_rewrite to change requests from

/some/static/page.html

to

/htmlinjector.php?url=/some/static/page.html

then use PHP (or whatever you find appropriate) to do the file-manipulation. Add an output cache to improve performance.

As an alternative, Apache Handlers sound helpful:

Modifying static content using a CGI script

The following directives will cause requests for files with the html extension to trigger the launch of the footer.pl CGI script.

Action add-footer /cgi-bin/footer.pl
AddHandler add-footer .html

Then the CGI script is responsible for sending the originally requested document (pointed to by the PATH_TRANSLATED environment variable) and making whatever modifications or additions are desired.

This is more or less what the mod_rewrite approach would do, only with less hackery.

link|improve this answer
feedback

Here is a tutorial on how to use mod_proxy_html to edit the links on a webpage ( the content). You might be able to apply this modify the html you want.

UPDATE: Are you sure you want to go this route? I think Apache is meant to serve content, not create it. This would probably go in the view part of a MVC framework. The reason I wouldn't recommend this is you are breaking the rule of modularity. Your web application will be intertwined with the application that server it, complicating future upgrades, moves, etc.

link|improve this answer
Hm, if I get it right mod_proxy_html is not capable to add complete new code snipets? – Node Jul 27 '09 at 12:38
Not sure, now that you have added more, Commander Tomalak's answer sounds better. You are in the land of hackery I would say, creating web pages isn't really what Apache is for, it is for serving up content. – Kyle Brandt Jul 27 '09 at 12:54
I tought that there is maybe some kind of standalone proxy which could rewrite html pages. Something like MySQL Proxy for MySQL. Maybe Privoxy could do this. – Node Jul 27 '09 at 13:03
@Kyle Brandt: LOL - I think the mention of the military rank officially makes you a trekkie. This made my day. :) – Tomalak Jul 27 '09 at 13:15
feedback

mod_sed is a good fit here. You can create an output filter that matches the closing head or body tag, for example, and insert your html before it.

link|improve this answer
feedback

I would prefer to do this with mod_rewrite and SSI.

First put the path into an environment variable

RewriteCond %{IS_SUBREQ} false
RewriteRule ^(/.*\.html) /page.shtml [E:filename:$1]

then process that in the shtml file

<!--#include virtual="$filename"-->

(parts of this solution are based on a stackoverflow question http://stackoverflow.com/questions/40133/getting-apache-to-modify-static-webpages-on-the-fly/1196832 )

link|improve this answer
My main reason to want to use shtml for this would be that calling an external CGI for each request might cause load problems. – Alex Lehmann Jul 28 '09 at 12:26
feedback

Would a mod_perl module be any use?

http://search.cpan.org/~gozer/Apache2-Layout-0.6/lib/Apache2/Layout.pm

That might do what you want or, at least, point you in the right direction.

link|improve this answer
Thx, I'll have a look, at the moment we tend to httpd.apache.org/docs/2.0/handler.html with a custom footer.pl – Node Jul 27 '09 at 13:55
feedback

you can look into the header and footer directive of apache, described below

http://wannabe.guru.org/scott/hobbies/apache/

link|improve this answer
feedback

You could use Apache mod_substitute to inject html into outgoing responses using some criteria or regular expressions. Here is an explanation of how to achieve this.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.