Thanks @thomaskr.
Rewrite rules may or may not work for every CDN, depends on CDN configuration. If “Vary Accept” header is sent from your server and your CDN service obeys it, correct image is sent to client browser.
https://www.igvita.com/2013/05/01/deploying-webp-via-accept-content-negotiation/
Unfortunately, detecting browser capability is not a problem that can be solved with simple and fast solution using either rewrite rules or PHP. Not all major browsers/versions will send “Accept: image/webp” header, even when they do support webp images.
Tag <picture> is planned for the future release, because it should work via every CDN. But, this solution still leaves out CSS or JS applied images as problem to solve with rewrite-rules (and HTTP-headers) whether using CDN or not.