14

I configured apache httpd to apply substitute. For my eyes it is exactly what the doc says. However it does simply nothing.

What is wrong with it?

<VirtualHost domain:443>
    SSLEngine on
    ....

    ProxyPass /cms/ http://domain2/
    ProxyPassReverse /cms/ http://domain2/
    Substitute "s|div|DIV|ni"
</VirtualHost>

(Apache 2.4.16 on Centos)

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
  • Brian Moon's comment [here](http://httpd.apache.org/docs/2.4/mod/mod_substitute.html) about why it didn't work for him might be worth checking out. – Pekka Sep 16 '15 at 08:22
  • Is this the real-world example though? That seems like a really odd thing to do, given the likely performance penalty and all... but then your case is probably more complex – Pekka Sep 16 '15 at 08:22
  • 1
    It is a sample to test. It is not the goal to uppercase div. We want to replace a URL which is part of inline scripts – stefan bachert Sep 16 '15 at 08:41

3 Answers3

17

Meanwhile I figured out how to make it work

<VirtualHost domain:443>
    SSLEngine on
    ....
# In some case the following line is neccessary
    RequestHeader unset Accept-Encoding

    <Location /cms >
         ProxyPass http://domain2/
         ProxyPassReverse http://domain2/
         AddOutputFilterByType SUBSTITUTE text/html
         Substitute "s|div|DIV|ni"
    </Location>
</VirtualHost>

I hope this helps others to overcome similar problems.

(We use it to exchange urls from a proxied source, after hard work apache httpd is a great tool ;-))

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
  • 2
    Thanks, I have spend many hours on this. The RequestHeader unset was what did it for me. – uncletall May 28 '19 at 14:57
  • ... Tore everything appart for an hour before finding this. But why does this work? Brotli and Gzip not supported? – Ray Foss May 22 '20 at 17:32
  • 1
    @RayFoss, the reason if because SUBSTITUTE filter acts after INLATE filter, and and that moment, SUBSTITUTE sees the "gziped" version of the page and not text. – pringi Jul 21 '22 at 14:41
  • Awesome find! Does anyone know a way to re-arrange the order of the filters so substitution would work without sacrificing compression? I haven't even found an easy way to debug the filter order :( `INFLATE;SUBSTITUTE;DEFLATE` didn't work for me and sounds inefficient even if it worked. – user193130 Apr 25 '23 at 02:54
7

This worked for me: AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html

see https://serverfault.com/questions/843905/apache-mod-substitute-works-in-curl-but-not-on-browser?newreg=c6eab8403f83476096a3d49dd64edeeb

rymach
  • 113
  • 1
  • 7
0

Thank you, stefan for finding the solution, I was stuck with the same issue.

For what it's worth, in my case, the back-end server is providing html but also some javascript, graphql end-point and other APIs, which required to also add other mime-types, and the final config would look like this:

<VirtualHost domain:443>
    SSLEngine on
    ....
    RequestHeader unset Accept-Encoding

    <Location /cms >
         ProxyPass http://domain2/
         ProxyPassReverse http://domain2/
         AddOutputFilterByType SUBSTITUTE text/html text/xml text/javascript application/json
         Substitute "s|div|DIV|ni"
    </Location>
</VirtualHost>
loic.jaouen
  • 489
  • 4
  • 9