Apache as Proxy replace the html code/tags/text

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?

Answer

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.

Attribution
Source : Link , Question Author : xmux , Answer Author : mtak

Leave a Comment