Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
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.
| |
|
Topic: mod_substitute not substituting |
|
Author |
|
csdude55
Joined: 22 Jan 2023 Posts: 23 Location: USA, Wilkesboro
|
Posted: Mon 08 Jul '24 6:47 Post subject: mod_substitute not substituting |
|
|
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: 348 Location: UK
|
Posted: Mon 08 Jul '24 22:21 Post subject: |
|
|
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
|
|
Back to top |
|
|
|
|
|
|