I’ve got a virtual host in my Apache config to deal with ads, spam, and malware sites. It works by having bad servers redirect to a specific loopback address that is mapped to the virtual host via the HOSTS file.
Using the following directives, I have been able to replace any pages from bad servers with something like [ad] and any graphics from bad servers with a local 1x1, transparent PNG file.
RewriteRule \.(gif|jpg|png|jpeg)$ /1x1-trans.png
ErrorDocument 404 "<p>[ad]</p>"
However recently, I have seen pages with broken IMG tags because they use a SRC without a file extension.
<img src="http://badserver.com/adsandjunk/foobar;tile=4;sz=575x90;othervariables=stuff?">
I tried using
RewriteRule ^.*$ "<p>ad</p>" [L]
But that gives the broken image placeholders again. Using this
RewriteRule ^.*$ /1x1-trans.png [L]
Fixes the images, but then any non-images (like pages, frames, etc.) pop up a Save As dialog for the PNG.
How can I get Apache to replace graphics (ie any IMG tag) with a graphic and everything else with a bit of HTML?
Thanks a lot.
Just out of curiosity, are you using Apache as a reverse proxy here? That's the only context I can understand you having a virtual host to "deal with ads, spam, and malware sites."
I'm not sure this is a mod_rewrite issue. You may be better off using filtering:
http://httpd.apache.org/docs/2.0/mod/mod_ext_filter.html
Particularly the section where they use sed to replace text. You can use just about anything really, perl, etc.
I have not done this myself, but the sed route looks promising if you can cobble together the specific search and replace criteria.
You're doing this the hard way. Just use Privoxy.