Forum Moderators: phranque
That said:
1) Use the AddHandler and Action directives; I've not tried in this .htaccess before, but I have used it in httpd.conf; provided the FileInfo option has been set for AllowOverride, this should work:
#
# set a handler for the file extensions you want this
# to apply to
AddHandler my_action .html .htm .php
#
# define the action to take for that handler
Action my_action /URI/to/myscript.php
The PHP script could just as easily be a CGI script (or possibly even an .shtml file); it just needs to be able to include the file you care about (php could 'require' or 'include' it, a CGI could take some other action). Just be sure that the script defined in 'action' doesn't have one of the file extensions you used in your AddHandler line, or Apache will loop internally.
2) use mod_rewrite
To affect all files:
RewriteEngine on
RewriteRule ^(.*) /path/to/targetscript.php?srcpage=%1 [L]
To affect all htm and html files:
RewriteEngine on
RewriteRule ^(.*)\.html? /path/to/targetscript.php?srcpage=%1 [L]
And, again, have targetscript.php do whatever processing needs to be done and include/require the original page (stored in the 'srcpage' element of the query string).
This is off the top of my head; these solutions don't take into account any query strings being passed to the original file; you may have to experiment a bit if you need to handle that eventuality. Good luck. =)
php_value auto_prepend_file /path/to/file/php_pre.inc
You can also inlcude one at the end of the page load with
php_value auto_append_file /path/to/file/php_post.inc
If you want a file in the middle of the page then you need to include and include directive in the page itself.
Of course this will only work for pages that are parsed by php
a) performance. Enabling AllowOverride for certain paths will result in a performance hit. I know that performance doesn't concern a lot of folks as much as it does me; I deal with a large amount of traffic in my day job. However, performance is more than just getting the user the data faster; if you can do that, you have a large capacity on existing hardware, which saves money.
b) Centralization. As soon as you start scattering .htaccess files all over the filesystem, that's one more thing that needs documentation (and we all *LOVE* writing documentation, don't we? =) ). Keeping all the configuration directives in one file (or, in the case of complex configs, a series of Include'd files, all in the same directory) makes everyone's life a bit easier.
All that said, what you basically want to do is prepend (or append) an HTML snippet (which may or may not include javascript) to all outgoing content. This can be done; the method I outlined above is one way to handle it. One could also make use of mod_perl, either by writing a small mod_perl handler which did what you needed, or by making use of HTML::Mason ([masonhq.com ]).
The thing to keep in mind is that Apache is a *webserver*, not a *content manager*; I'm not aware of directive that says "send this little file down the wire in addition to the reqeuested file every time a file of a certain type is requested" (although I'd be interested if there were). It's not (natively) designed to be able to manipulate the content it's returning. It provides a server-side includes mechanism which gives developers some additional abilities, and can be expanded to do pretty much anything you want, provided you're willing to expend the dev cycles to do it.