Well, it seems you've got a working solution so there's no need for a 'correction'. However, I can explain why an infinite loop was happening in the first snippet.
The RewriteRule is saying, "if the requested file
doesn't end in .gif, ... .css then go to /test/index.php". However, when the request is made to index.php it
doesn't end in .gif, ... .css and so redirects another time to /test/index.php. And because index.php doesn't end in.... You get the idea.
A method that I use is slighly different and may or may not be more beneficial for you would be something along the lines of:
PHP Code:
# Rewriting requests for pretty URLs
RewriteEngine On
# Allowed request are from 1 to 3 levels deep:
# domain.com/section
# domain.com/section/page
# domain.com/section/page/cruft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/rewrite(/[a-zA-Z0-9_\-]+){1,3}/?$
RewriteRule ^([^/]+)/?$ index.php?sec=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?sec=$1&page=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?sec=$1&page=$2&etc=$3 [QSA,L]
A little more confusing, perhaps, but it should be a good starting (or ending!) point for you.