Technical Tip: How to perform a search and replace in HTML Body text
Description
This article describes that when implementing FortiWeb as a proxy, there may be a need to replace text in the HTML body so that the HTML from FortiWeb is presented correctly to the user.
For example, when offloading to the FortiWeb, the server can often return the URL in the form HTTP rather than HTTPS in the HTML, as the FortiWeb is connected as HTTP.
For example, when offloading to the FortiWeb, the server can often return the URL in the form HTTP rather than HTTPS in the HTML, as the FortiWeb is connected as HTTP.
Scope
FortiWeb.
Solution
The following steps can be used to enable a rewrite of the HTML body text from HTTP to HTTPS:
- Configure the maximum cache for FortiWeb to be able to manipulate the text from the back-end server.
config system advanced
set max-cache-size 1024
end
- Set up an uncompress rule to enforce that the HTML is received as text and not compressed. (A common reason why the text is not detected and changed).
config waf web-protection-profile inline-protection
edit "rewrite"
set http-session-management enable
[deleted ....]
set file-compress-rule CompressZ
set file-uncompress-rule UnCompressZ
next
end
- Collect a packet capture matching the client's IP address at Network > Packet Capture to check the traffic that needs to be replaced.
Note: If traffic is encrypted using HTTPs, the keys for decryption can be captured by enabling while collecting the PCAP and decrypting traffic. For the required steps, see the following documents:
- Enabling diagnose debug flow to retrieve TLS Pre-master secrets
- Decrypting TLS 1.2/1.1/1.0 Traffic
- Decrypting TLS 1.3 Traffic
- Create a URL rewrite rule to have a regex to detect the 'http://' and replace it with the 'https://'.
config waf url-rewrite url-rewrite-rule
edit "rewrite"
set location http://
set body_replace https://
set action http-body-rewrite
config match-condition
edit 1
set object http-body
set reg-exp "http:\\/\\/"
set protocol-filter enable
set content-type-filter enable
set content-type-set text/html text/plain text/javascript application/xml(or)text/xml application/javascript application/soap+xml application/x-javascript application/json application/rss+xml
next
end
next
end
- Create a URL Rewrite Policy and assign the Rule.
config waf url-rewrite url-rewrite-policy
edit "rewrite"
config rule
edit 1
set url-rewrite-rule-name rewrite
next
end
next
end
It is also possible to configure a URL rewrite rule and policy from the GUI as follows:

- Assign the URL Rewrite Policy to the Web Protection Profile and then to the Server Policy.
Note:
When testing the problem, it appears not to work because of the Cache of the Browser.
