This works for a cookie like id=1234
:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^id=([0-9]*)$ [NC]
RewriteRule .* http://localhost/mysite/cache/%1 [R=301,L]
RewriteRule .* http://localhost/mysite/cache/guest [R=301,L]
Now for your problem:
Make sure that your htaccess does not apply to the page you rewrite to! For example, if your .htaccess lies in /mysite/.htaccess
It will be used again in
http://localhost/mysite/cache/%1
That’s maybe the reason for your infinite loop. To resolve this, either make sure the htaccess rules are not applied to the sub-directories or use another directory for the cache.