Q:

Mod rewrite only when directory exists

I have a directory structure as follows:
/gallery
----index.php
----/23XASDTAGH
----/24XGA43KJA/

I'd like to use mod rewrite to rewrite if a directory exists. so:
www.example.com/gallery/23XASDTAGH/
becomes
www.example.com/gallery/index.php?gallery=23XASDTAGH
but i'd like to do this silently, so no changes happen to the url. now i have this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}/ -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]

which works with
www.example.com/gallery/23XASDTAGH/
but the odd thing is when i leave the trailing slash off the end it changes the url to
www.example.com/gallery/23XASDTAGH/?gallery=23XASDTAGH
how can i get it to work with or without the trailing slash? 

A:

Finally figured it out, i needed a ?/ at the end of my expression in the rewrite rule to say that the slash was optional. Why do regular expressions have to be so hard easy. 8-)
correct expression:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}     -d
RewriteRule ^([a-zA-Z0-9_-]+)?/$    /gallery/index.php?gallery=$1 [L]

