Using mod_rewrite, what RewriteRule(s) will make the next two good examples function correctly?

Example #1 (one term):

http://example.net/dir/thanks
-to-
http://example.net/dir/index.php?a=thanks

Example #2 (two terms):

http://example.net/dir/thanks=stackoverflow
-to-
http://example.net/dir/index.php?a=thanks&b=stackoverflow

Note: The .htaccess file is situated in /dir/ outdoors of domain root.

I have become close with this particular:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)=([^/]*)/?$ index.php?a=$1&b=$2 [L]

However, to utilize example #1 a trailing "=" is needed (which I wish to avoid). I have attempted altering "=" to "=?" within the regex to really make it optional but although it then works for instance #1 it fails for instance #2.

Thanks kindly for just about any consideration to my question. I am a new comer to mod_rewrite and regex and am truly stumped.

Why don't you create two seperate rewrite rules:

i) One which takes proper care of example 1 - using the added make sure that it doesn't contain the same sign [to ensure that you do not match example 2 with rewrite rule 1]

ii) A different one that can take proper care of example 2

I see two primary approaches:

1. More safer/simpler to know and extend if required. Make two rules: first will catch thanks=stackoverflow while second works with thanks only:

RewriteEngine On

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# will work with /thanks=stackoverflow
RewriteRule ^([^/=]+)=([^/=]+)/?$ index.php?a=$1&b=$2 [L]
# will work with /thanks
RewriteRule ^([^/=]+)/?$ index.php?a=$1 [L]

2. Mix individuals two rules right into a single rule. Within this situation parameter b= will be present, and can be empty for thanks scenario:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/=]+)(=([^/=]+))?/?$ index.php?a=$1&b=$3 [L]

Examined both -- working fine on my small Apache box.