Forum Moderators: phranque

Message Too Old, No Replies

ENV variable not being read in RewriteRule

ignores the variable i input

         

mikes300

9:35 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



Using this:

SetEnv HostServer "testserver"
RewriteRule ^(.*)/test {ENV:HostServer}/admin [L,R=301]


This works. However I do not think it is even reading the HostServer variable at all, just sending it to the relative /admin link. To prove this i just set the HostServer variable to something bogus and it still works just fine.

Am I missing something simple about how such a variable should be picked up?

For the longest time here I thought this was actually working until figuring out it was just going to the relative path, essentially ignoring the variable. Thanks in advance to whoever can help!

jdMorgan

4:10 am on Feb 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Missing character:

SetEnv HostServer "testserver"
RewriteRule ^(.*)/test [b]%{E[/b]NV:HostServer}/admin [L,R=301]

Note also that the same can be accomplished with

RewriteRule ^ - [E=HostServer:testserver]
#
RewriteRule ^(.*)/test %{ENV:HostServer}/admin [L,R=301]

and that the flag method shown in the first rule (which currently does nothing but set the HostServer flag) can be used with a more-selective rule if desired. For example, you could use a RewriteCond to look at the Remote_Addr variable, and define the HostServer name based upon who is sending the request.

Jim

mikes300

2:49 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



Thank you. That missing character was a misstype in my post. This does do the trick but do you know if RewriteRules in a VirtualHost stanza will completely trump any set outside of it (globally)? I ask because i set that variable inside the vhost stanza and the subsequent rewrites see the variable. I set the exact same statement immediately outside the vhost stanza and it does not apply this rule. Verified this in the rewrite log.

jdMorgan

3:56 pm on Feb 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not really sure about the variable scope in that case, although like you I *would* expect it to work, since both the RewriteRUle and SetEnv directives' contexts are documented as "server config, virtual host, directory, .htaccess" and your variable name does not apparently "collide" with any server-defined variable names.

If you've got anything running on this server that 'sanitizes' variables (such as SUexec), then that may be the problem. But this is just a guess.

Jim