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.

10 Answers 10


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




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.

  • Can someone give an example of the mod_rewrite solution? (specifically the content of htmlinjector.php)
    – Asaf
    May 9, 2016 at 15:24
  • 1
    For those of you struggling with this - don't forget ScriptAlias and <Directory /some/static/>...</Directory>, <Directory /path/to/cgi-bin/> Options +ExecCGI</Directory>etc. I don't want to tamper with the user's answer but if you request, I will give more info. Oct 23, 2016 at 4:53
  • @asdf Example: <?php ob_start(); include("/some/static/page.html"); $contents = ob_get_contents(); ob_end_clean(); echo str_replace('</head>', '<script async>Some script content goes here like adsense code</script></head>', $contents) ?> use $content to load the rest of the page content. you can save this file as htmlinjector.php or whatever and call it in your htaccess file like this: Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} /some/static/page.html RewriteRule ^(.*)$ /htmlinjector.php?url=/some/static/page.html Aug 19, 2018 at 10:25

I am not sure why this hasn't been mentioned in the list of answer. Sorry if it took me 2 years to see this question...

The easiest, most powerful way to do what you want to do what you want is using an Apache filter.

Just have:

ExtFilterDefine css_changer mode=output intype=text/html cmd="/some/php/script.php"
SetOutputFilter css_changer

A possible script:


#phpinfo(); // Uncomment to see ALL env variables
$host = $_ENV["HTTP_HOST"]; // www.site.com
$script_name = $_ENV["SCRIPT_NAME"]; // /theme/green/style.css
$pi = pathinfo($script_name);
$type = $pi['extension'];
#print "$host $script  $type";

$stdin = STDIN;

while($line = fgets($stdin)){
  $line = preg_replace('/a/', 'A', $line);

  fwrite(STDOUT, $line);

This will change all "a"s into "A"s .

Be sure to enable filter in your httpd.conf, like this:

LoadModule ext_filter_module libexec/apache2/mod_ext_filter.so

This question ranks really up in Google and there isn't much out there in terms of forums

  • 1
    That's by far the best solution, i was able to find for a similar problem. Thanks very much for sharing!
    – harald
    Dec 18, 2012 at 11:48

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.

  • Hm, if I get it right mod_proxy_html is not capable to add complete new code snipets?
    – Node
    Jul 27, 2009 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. Jul 27, 2009 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, 2009 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, 2009 at 13:15

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.


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: https://stackoverflow.com/questions/40133/getting-apache-to-modify-static-webpages-on-the-fly/1196832 )

  • 1
    My main reason to want to use shtml for this would be that calling an external CGI for each request might cause load problems. Jul 28, 2009 at 12:26

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 is dead,,,
    – Orphans
    Dec 5, 2022 at 21:30

Would a mod_perl module be any use?


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


you can look into the header and footer directive of apache, Using the directives below.

<Directory "/usr/local/www/data/scott">
    Options +Indexes
    AllowOverride All
    AddOutputFilter LAYOUT html htm shtml
    LayoutComment On
    LayoutHeader /wrappers/scott-header.html
    LayoutFooter /wrappers/scott-footer.html

More reading is here: http://wannabe.guru.org/scott/hobbies/apache/


Mod Layout is now obsolete and if you don't want the overhead of calling an external script, the best solution which worked for me was mod sed. You can use mod sed to match first line of doc (1s) and add header script code there and match last line ($s) and put footer there.

Options Indexes FollowSymLinks Includes ExecCGI
Order Deny,Allow
Deny from none
Allow from all
Require all granted

AddOutputFilter Sed html
SetOutputFilter Sed;DEFLATE
OutputSed "1s|^|<header code>|"
OutputSed "$s|$|</footer code>|g"
  • On a shared hosting server this may not be available. Anyway how would one find the closing body tag and insert a Google Analytics code just before it?
    – Khom Nazid
    Mar 19, 2022 at 13:08

i can insert html to all pages via Apache, but only for site that hosted by my apache, not all site though my apache proxy server.

this is how it work. i set up an XAMPP, download mod_layout 5.1 for apache 2.4. (install mod_layout is simple, if you use XAMPP for windows, just download mod_layout.so 5.1 for windows and put it to your apache module folder $home/apache/module, then add the config DSO to your httpd.conf LoadModule layout_module modules/mod_layout.so - if you using linux or other os, you should download mod_layout.so for linux, then run make command to install)

After install mod_layout.so for windows, just put this code on your .htaccess file or httpd.conf your will get html insert to all your page:

<IfModule mod_layout.c>
AddOutputFilter LAYOUT html 
AddOutputFilter LAYOUT htm 
AddOutputFilter LAYOUT shtml 
AddOutputFilter LAYOUT shtm 
AddOutputFilter LAYOUT cgi 
AddOutputFilter LAYOUT php 
LayoutFooter "C:/xampp/apache/cgi-bin/footer.php"

C:/xampp/apache/cgi-bin/footer.php is where you put your html or php file, in my situation is C:/xampp/apache/cgi-bin/footer.php, but you can put it any where, just give the right path, you'll be fine

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .