Forum Moderators: phranque

Message Too Old, No Replies

Setting and reading an environment variable

.htaccess problem

         

musicmaster

12:57 pm on Mar 15, 2009 (gmt 0)

10+ Year Member



Hi,
I want to apply the 'standard' transformation of "mydomain/var1/var2" into "mydomain?a=var1&b=var2" complicated by the addition of a flag variable.

In my first rule I take the flag from the string. So "mydomain/var1/var2/~myflags/aa" becomes "mydomain/var1/var2/aa" and the flags are stored into environment variable flagz.

RewriteRule ^(.*)~([^/]+)/(.*) $1$3 [E=flagz:$2]

Next this variable is taken up in the final transformation:

RewriteRule ^([^/]*)/([^/]*)/([^/]+) $3.php?a=$1&b=$2&flags=%{ENV:flagz} [L]

It looks like "[E=flagz:$2]" doesn't work, but I don't know what is wrong.

Thanks

jdMorgan

2:54 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't really see a problem, although you may be seeing recursion due to the "*" quantifiers, which would allow blank parameters to match.

But it seems that the whole thing could be done in one step, avoiding input-URL ambiguity and the need to chain two rules with a [C] flag:


RewriteRule ^([^/]+)/([^/]+)/~([^/]+)/([^/]+)$ $4.php/?a=$1&b=$2 [E=flagz:$3,L]

This takes the requested URL-path /var1/var2/~myflags/aa" and rewrites it to call /aa.php with a=var1&b=var2 and with your flagz envar set to "myflags". It is more specific, in that it requires the requested URL-path to fully match the "/w/x/~y/z" format before either setting the flagz envar or invoking the script; No trailing slashes or directory levels will be accepted, so this prevents having problems with this rule executing unexpectedly (now or in the future) with incorrectly-formatted, invalid, or deeper-directory input URL-paths.

Jim

g1smd

6:15 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you do have a requirement for a shorter version of URL (i.e. one with less parameters) to be rewritten, then the addition of another, and equally exact-matching, rule could separately cover that usage.