Apache as Proxy replace the html code/tags/text

2

3

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

Posted 2014-06-11T07:34:15.253

Reputation: 229

Answers

7

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.

mtak

Posted 2014-06-11T07:34:15.253

Reputation: 11 805

Thank 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. – Yash Agarwal – 2015-05-19T10:01:27.337

Getting an error on the first FilterProvider line: "FilterProvider takes three arguments, filter-name provider-name match-expression" – cmcginty – 2016-02-05T00:50:06.027

Yep 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 – 2014-06-11T13:56:18.633

And also my proxy.conf file looks like that: ProxyRequests On ProxyVia On <Proxy *> Order deny,allow Deny from all Allow from all </Proxy> – xmux – 2014-06-11T13:59:33.520

@xmux I've updated my post with a config that is tested and works for me. – mtak – 2014-06-11T14:21:18.397

Thanks! 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 – 2014-06-11T14:26:13.493

It is a piece of the total Apache config. If you put this anywhere in your Apache config (or in /etc/(apache2|httpd)/sites-enabled), it will make virtualhost su-test.int.mtak.nl a proxy. – mtak – 2014-06-11T14:29:17.737

2You 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. – xmux – 2014-06-11T14:44:38.190

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|"

cmcginty

Posted 2014-06-11T07:34:15.253

Reputation: 2 249