7

I’m trying to better understand Apache’s use of a binary attribute. In an .htaccess file with the code

# Check if HTTPS is 𝗻𝗼𝘁 𝗼𝗻
RewriteCond %{HTTPS} !=on 

Is there more processing occurring – at any level – than there is with the similar code

# Check if HTTPS is 𝗼𝗳𝗳
RewriteCond %{HTTPS} off

2 Answers 2

5

tl;dr In this example, the net result is the same, but the pattern !=on is arguably more efficient because it is a lexicographical string comparison, not a regular expression. Unlike off, which is processed as a regex.

However, in real terms, there is no measurable performance difference between these particular expressions. It's a matter of personal preference which one you choose. (You could also use !on instead.)

Note that the HTTPS server variable should always be set. It is set to either off or on (lowercase) exactly. The regex off checks that the value is set to "off" (or, strictly speaking contains "off"). However, the regex !=on (or !on) is successful whenever the value is not "on", so if the HTTPS server variable was not set at all (for whatever reason) or set to "something" else entirely then it would also be successful - which wouldn't necessarily be correct. However, as noted, the HTTPS server var should always be set and never be anything other than on or off. So in this instance, it should make no difference.


Apache’s use of a binary attribute...

This isn't strictly a "binary attribute". All Apache server variables contain strings. In the case of the HTTPS server variable, this is set to either the string "on" or "off" (always lowercase).

RewriteCond %{HTTPS} !=on 

(Note this has a different meaning to !on.)

  • The ! prefix (which can be prefixed to any condition) negates the result of the condition.

  • The important part here is the = operator. This changes the expression from being a regex comparison to a lexicographical string comparison for equality. The fact that it is no longer a regex is what arguably makes it "more efficient" (although in real terms, there is no measurable performance difference in this example). So, =on checks that the server variable HTTPS is exactly equal to "on". The ! prefix then negates the result. Whereas off checks that HTTPS contains the string "off" anywhere (the same as ^.*off.*$).

The comparable expression to !=on is really =off, not off (as in your example). And off is comparable to !on. The fact that you rarely see =off is just human nature, personal preference, habit, copy/paste, readability, misunderstanding (take your pick)?

In summary:

# Check if HTTPS is not exactly equal to "on" (lexicographical comparison)
RewriteCond %{HTTPS} !=on 

vs

# Check if HTTPS contains the string "off" anywhere (regex)
RewriteCond %{HTTPS} off
0

Not Equal to & Not

A short answer - No they are the same. != is an operator that means not equal to. Therefore !=on and off mean the same in the .htaccess code.

In English, the two examples you provided mean:

If not equal to https

or

If https is off

They are just different ways of writing the same thing.


Related:

What is the origin of != in the meaning “not equal to”?

1
  • "!= is an operator" - in Apache speak, != isn't strictly the operator. The operator is simply =, with a ! prefix. The negation prefix (!) can be applied to any condition in order to negate the result: !on, !<foo, !-f, etc.
    – MrWhite
    Commented Oct 13, 2018 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.