Using mod_rewrite, what RewriteRule(s) would make the following two examples function properly?
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 located in /dir/ outside of domain root.
I’ve gotten close with this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)=([^/]*)/?$ index.php?a=$1&b=$2 [L]
However, to work with example #1 a trailing “=” is required (which I want to avoid). I’ve tried changing “=” to “=?” in the regex to make it optional but while it then works for example #1 it fails for example #2.
Thank you kindly for any consideration to my question. I’m new to mod_rewrite and regex and am truly stumped.
I see two main approaches:
1. More safer/easier to understand and extend if necessary. Make two rules: 1st will catch
thanks=stackoverflowwhile 2nd will work withthanksonly:2. Combine those two rules into a single rule. In this case parameter
b=will always be present, but will be empty forthanksscenario:Tested both — working fine on my Apache box.