It’s not possible using Cloudflare Rules, such as Page Rules or Redirect Rules, as they would need to be based on the response, at which point in time they have already been triggered.
If you are (or have access to) a JavaScript developer, you may want to consider Workers to perform that function. If you are so inclined, please open a new topic under Developers > Workers to ask for help.