Apache as Proxy replace the html code/tags/text

7,888

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|"
Share:
7,888

Related videos on Youtube

xmux
Author by

xmux

Updated on September 18, 2022

Comments

  • xmux
    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
    xmux almost 10 years
    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
    xmux almost 10 years
    And also my proxy.conf file looks like that: ProxyRequests On ProxyVia On <Proxy *> Order deny,allow Deny from all Allow from all </Proxy>
  • mtak
    mtak almost 10 years
    @xmux I've updated my post with a config that is tested and works for me.
  • xmux
    xmux almost 10 years
    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
    xmux almost 10 years
    You 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
    Yash Agarwal almost 9 years
    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.
  • cmcginty
    cmcginty about 8 years
    Getting an error on the first FilterProvider line: "FilterProvider takes three arguments, filter-name provider-name match-expression"