logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Third-party Modules View previous topic :: View next topic
Reply to topic   Topic: mod_substitute not substituting
Author
csdude55



Joined: 22 Jan 2023
Posts: 23
Location: USA, Wilkesboro

PostPosted: Mon 08 Jul '24 6:47    Post subject: mod_substitute not substituting Reply with quote

I have this test in a .CONF file:

Code:
RewriteEngine on

<Location "/">
 RewriteRule ^ - [E=foo:bar]

 AddOutputFilterByType SUBSTITUTE text/html
 Substitute "s/body/BODY/i"
</Location>




And I'm using this basic PHP script to see the results:

Code:
<?php
echo <<<EOF
<html>
<head>
 <title>Test</title>
</head>

<body>
<pre>

EOF;

print_r($_SERVER);

echo <<<EOF

</pre>
</body>
</html>
EOF;
?>



I see that $_SERVER['foo'] equals "bar", so the configuration is working properly and <Location> is matching. But "body" isn't being replaced with "BODY".

I'm using Apache 2.4.59, and EasyApache (via WHM) definitely shows that mod_substitute is installed. And I would assume that, if it wasn't, I would get an error when I rebuild Apache.

I double checked using Google's Dev Tools > Network > Name > Response Headers, and "Content-Type" shows:

text/html; charset=UTF-8

I'm running out of ideas! LOL Any other thoughts on why it's not substituting as expected?
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 335
Location: UK

PostPosted: Mon 08 Jul '24 22:21    Post subject: Reply with quote

I can't reproduce your problem in a minimal test environment (Apache 2.4.59); the substitute works for me.

Using your configuration (without the PHP backend), whilst changing BODY to BoDy, viz:
Code:
    <Location "/">
        RewriteRule ^ - [E=foo:bar]

        AddOutputFilterByType SUBSTITUTE text/html
        Substitute "s/body/BoDy/i"
    </Location>

when I access the site root and get the default "It works!" page, browser Dev Tools show the following raw response:
Code:
<html><BoDy><h1>It works!</h1></BoDy></html>

In the past I have had problems with AddOutputFilterByType, with various media types, particularly when using more than one filter, e.g. substitute and compression. As a result I switched to using filter directives, not least of which you can select the FilterProvider using regular expressions; for example the following substitute filter matches a range of content types substrings:
Code:
FilterDeclare Replace
FilterProvider Replace Substitute "%{CONTENT_TYPE} =~ m#^text/(css|html|javascript|plain|xml)#i"
FilterProtocol Replace change=yes
FilterChain +Replace

Thereafter you declare the substitute directive as before (less the AddOutputFilterByType):
Code:
    <Location "/">
        RewriteRule ^ - [E=foo:bar]

        Substitute "s/body/BoDy/i"
    </Location>

Accepting this revised approach is more complicated, depending on the rest of your configuration, it may work for you.
Back to top
csdude55



Joined: 22 Jan 2023
Posts: 23
Location: USA, Wilkesboro

PostPosted: Tue 09 Jul '24 18:42    Post subject: Reply with quote

Thanks for the reply, tangent!

I stumbled across another solution that solved it, apparently the issue is between mod_substitute and gzip! So using this worked:

Code:
AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html


https://serverfault.com/questions/843905/apache-mod-substitute-works-in-curl-but-not-on-browser

I'm not sure if this will affect the load time of the page, though.
Back to top


Reply to topic   Topic: mod_substitute not substituting View previous topic :: View next topic
Post new topic   Forum Index -> Third-party Modules