Forum Moderators: phranque

Message Too Old, No Replies

SetEnv vs SetEnvIf vs RewriteRule, why use SetEnv if it runs last?

         

csdude55

6:59 pm on Jan 26, 2023 (gmt 0)

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



We've touched on this in other threads, but I wanted to clarify without taking them off topic.

It's my understanding that mod_rewrite (eg, RewriteRule) runs in the order it's presented. mod_setenv (eg, SetEnvIf) runs before everything else, and mod_env (eg, SetEnv) runs after everything else. So if I have this:

SetEnv this that
ReWriteRule ^ - [E=foo:bar]
SetEnvIf ^ ^ lorem=ipsum

then lorem is set first, foo is set second, and this is set last. Right?

Assuming so:

1. I recognize that SetEnv is subjectively easier to read than RewriteRule, but is there any other advantage to it? I guess if you have a reason to write it higher in the script but intentionally want it to run last, but I can't think of any example of this where it couldn't just be written at the end.

2. SetEnvIf is shorter than a series of RewriteCond => RewriteRule, but it seems like using it just once means that I need to use it exclusively in order to prevent confusion. Is there a DISadvantage to using SetEnvIf ^ ^ foo=bar (which should match in all cases, making it seem identical to SetEnv or RewriteRule other than the order of operation)?

I'm sure that Apache had some reason for creating the modules to run in this order, but I honestly don't understand it. It feels highly inefficient if Apache has to scan the entire config for any SetEnvIf, then scan again for any RewriteRule, then scan a third time for any SetEnv.

csdude55

9:03 pm on Feb 14, 2023 (gmt 0)

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



Gotcha. Which means that if I change RewriteRule to SetEnvIf in 10 then I'm probably going to have to change everything everywhere.

Blerg.

lucy24

9:17 pm on Feb 14, 2023 (gmt 0)

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



Yes, but it will be to your long-term advantage. SetEnvIf is easy to work with.
This 32 message thread spans 2 pages: 32