Using Apache's mod rewrite how to redirect depending the query string?
Using Apache's mod rewrite how to redirect foo.php?r=ok
to boo.php
(that is, depending the query string: if r
= ok
the开发者_JAVA百科n redirect).
you can't use query string (the string after "?" character) as variable for rewrite, because query string is not a part of URL path.
you need to modify foo.php?r=ok to some string path like "foo.php/redirect/"
From the Apache manual discussing mod_rewrite:
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Look closely at RewriteRules and RewriteConditions
EDIT: Added a working example
The RegEx use to write these is just pure heaven </sarcasm>
Any how... here's a simple rule (Apache2, Mac OS X 10.5.8)
RewriteCond $1 ^(foo\.php)
RewriteCond %{QUERY_STRING} (r=ok)
RewriteRule (foo\.php) /bar.php [R]
I tested this for as many obvious caveats that I could think of (ie r=ko, oof.php, foo.php?r=o), but couldn't break it.
精彩评论