1. The problem I’m having:
Using caddy with the replace_response module, I want to match and/or replace HTML code (not only text).
Replacing simple text strings works well (therefore I know that the module works) but I cannot get it to match or replace any HTML tags that contain special characters, such as e.g. <title>
or <br/>
. These are instead replaced as escaped HTML and thus rendered as text.
2. Error messages and/or full log output:
Example from my Caddyfile:
replace {
"Hello" "<b>Hello</b>"
}
will result in <b>Hello</b>
in the page’s source code (inspected with the developer tools / F12 → Edit as HTML) on the website which is then rendered in the browser as
<b>Hello</b> User, welcome to this page!
instead of (desired outcome):
Hello User, welcome to this page!
I have also tried:
- using different ways to escape including:
\ ' " and
` - the
replace stream
option (instead of justreplace
), - as well as toggling
encode gzip
- and
header_up Accept-Encoding identity
None of them seem to make a difference.
3. Caddy version:
v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=
4. How I installed and ran Caddy:
Just downloaded exe from here and ran it in elevated cmd
a. System environment:
Windows 10 Pro x64 22H2 Build 19045.4894
b. Command:
caddy run --watch
d. My complete Caddy config:
My caddyfile:
{
order replace after encode
}
https://localhost {
#encode gzip
reverse_proxy https://192.168.1.2 {
# header_up Accept-Encoding identity
transport http {
tls_insecure_skip_verify
}
}
replace {
"Good morning" "Good night"
"Latest News" "<b>Latest News</b>"
`<img src="hero.jpg">` `<img src="hero_new.gif">`
`My books and more` `My books <br/> and more`
"<i>Log out</i>" "<sup>Good bye</sup>"
}
}
You can see that I have tried different options to escape the strings.
Only the first replacement (Good morning → Good night) works perfectly, all others fail in the above-described manner (don’t match if containing HTML in the pattern, or HTML is escaped and displayed as text when used in the replacement).
5. Links to relevant resources:
Documentation of the replace_response handler module, sadly does not contain any examples on how to replace strings with HTML tags or code.
I feel like it must be a simple and obvious oversight on my end that is causing this, so I’d be grateful if you could just show me a working example of how to do this right.
Thank you.