Apache as Proxy replace the html code/tags/text
Solution 1
You should be able to use mod_substitute. In your proxy config, add:
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s/foo/bar/ni"
I got it working with the following config:
<VirtualHost *:80>
ServerName su-test.int.mtak.nl
ProxyRequests Off
ProxyPreserveHost Off
ProxyPass / http://mtak.nl/
ProxyPassReverse / http://mtak.nl/
RequestHeader unset Accept-Encoding
FilterDeclare CUSTOMFILTER
FilterProvider CUSTOMFILTER SUBSTITUTE resp=Content-Type $*
FilterProvider CUSTOMFILTER SUBSTITUTE resp=Content-Type $/html
<Location />
FilterChain CUSTOMFILTER
Substitute "s|foo|bar|ni"
</Location>
</VirtualHost>
The line RequestHeader unset Accept-Encoding
is to make sure the webserver doesn't send a gzipped response, which Apache would be unable to substitute the contents of.
Solution 2
For Apache >= 2.4 the FilterProvider
syntax was changed. I was able to get the following to work:
FilterProvider CUSTOMFILTER SUBSTITUTE "%{CONTENT_TYPE} =~ m|^text/html|"
Related videos on Youtube
xmux
Updated on September 18, 2022Comments
-
xmux over 1 year
I configured my Apache server as a proxy server. I added some filters to my
proxy.conf
file to change the text (HTML source code) of the websites.Example code:
ExtFilterDefine foodo mode=output intype=text/html cmd="/bin/sed -r 's/foo/newfoo/g'" SetOutputFilter foodo
I have enabled also every possible mods for that. (
mod_proxy
,mod_proxy_html
, ...)After I used also
mod_sed
to change some text but still I have no positive solution.<Directory "/var/www/docs/sed"> AddOutputFilter Sed html OutputSed "s/monday/MON/g" OutputSed "s/sunday/SUN/g" </Directory>
Here is my proxy.conf:
ProxyRequests On ProxyVia On <Proxy *> Order deny,allow Deny from all Allow from all </Proxy>
Do anyone have any idea for this problem?
-
xmux almost 10 yearsYep i tried that also today!
sudo a2enmod substitute
and then<Location /> AddOutputFilterByType SUBSTITUTE text/html Substitute s/foo/bar/ni </Location>
and then i restarted my apache server! Then when i visited the website from the client side and look up the source code "foo" is still there. -
xmux almost 10 yearsAnd also my proxy.conf file looks like that:
ProxyRequests On ProxyVia On <Proxy *> Order deny,allow Deny from all Allow from all </Proxy>
-
mtak almost 10 years@xmux I've updated my post with a config that is tested and works for me.
-
xmux almost 10 yearsThanks! but then this one is the 000-default.conf file and i want still a proxy server actually. And i am using apache 2.4, does it make any difference?
-
xmux almost 10 yearsYou were totaly right about this "RequestHeader unset Accept-Encoding" after that i just updated my proxy.conf with <Location /> AddOutputFilterByType SUBSTITUTE text/html Substitute s/foo/bar/ni </Location> and it just worked! Thanks a lot! if you want, you can change the answer to a normal substitute example.
-
Yash Agarwal almost 9 yearsThank you so much for this answer. I had a feeling that gzip was causing the issue so I tried AddOutputFilterByType INFLATE to fix this. But although now response wasn't gzipped still substitute was not working. I tried you technique and it worked.
-
cmcginty about 8 yearsGetting an error on the first FilterProvider line: "FilterProvider takes three arguments, filter-name provider-name match-expression"