I was wondering how to create and debug this kind of script that can become a bit of headache if you are not used to write them (like me).
Do you use tool to create
You can set RewriteLog directive in your virtualhost configuration It will write necessary info to the file specified by you.
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
Further, use RewriteLogLevel directive to control the amount of logging
RewriteLogLevel 3
read through