RewriteEngine On
RewriteBase /
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=CWD:%2]

RewriteCond %{REQUEST_FILENAME} .*\.svg
RewriteRule ([^~]+)~v([0-9a-z]+)?\.svg$ %{ENV:CWD}svg.php?f=$1.svg&v=$2 [L,nocase]
RewriteRule ([^~]+)\.svg$ %{ENV:CWD}svg.php?f=$1.svg [L,nocase]
