2

I find apache very unfriendly.

Anyway. I have this:

RewriteRule (.*) - [QSA,E=DOMAIN_NAME:localhost.com]

This doesn't redirect, which is fine:

RewriteCond "www.%{ENV:DOMAIN_NAME}" "!=www.localhost.com"
RewriteRule ^ https://%{HTTP_HOST}?err=%{ENV:DOMAIN_NAME} [L,R=301]

This doesn't redirect, which is fine:

RewriteCond "%{HTTP_HOST}" "!=www.localhost.com"
RewriteRule ^ https://%{HTTP_HOST}?err=%{ENV:DOMAIN_NAME} [L,R=301]

This does redirect:

RewriteCond "www.%{ENV:DOMAIN_NAME}" "!=%{HTTP_HOST}"
RewriteRule ^ https://%{HTTP_HOST}?err=%{ENV:DOMAIN_NAME} [L,R=301]

Is very frustrating. I'm stuck in checking two variables if they match...

2
  • What exactly are you trying to do?
    – davidgo
    Jan 19, 2021 at 2:59
  • @davidgo I'm trying to check if www.%{ENV:DOMAIN_NAME} contains the same value as %{HTTP_HOST}
    – Andrei
    Jan 19, 2021 at 3:00

1 Answer 1

1

This does redirect:

RewriteCond "www.%{ENV:DOMAIN_NAME}" "!=%{HTTP_HOST}"

Because server variables of the form %{VARNAME} are not expanded in the CondPattern (2nd argument to the RewriteCond directive). You are comparing against the literal string "%{HTTP_HOST}", which is obviously different to "www.localhost.com", so the condition is successful.

You need to use a regex with an internal backreference instead. For example:

RewriteCond www.%{ENV:DOMAIN_NAME}@%{HTTP_HOST} !^([\w.]+)@\1$

Where @ is just an arbitrary character that does not occur elsewhere in the string being checked. And \1 is a backreference to the captured subpattern, ie. ([\w.]+).

Or use an Apache Expression (Apache 2.4):

RewriteCond expr "'local.%{ENV:DOMAIN_NAME}' != %{HTTP_HOST}"

Aside:

RewriteRule (.*) - [QSA,E=DOMAIN_NAME:localhost.com]

If you are simply setting an env var then you don't need to capture (or even match) the entire URL-path and the QSA flag is superfluous (since there is no substitution). For example:

RewriteRule ^ - [E=DOMAIN_NAME:localhost.com]
1
  • How did you get on with this?
    – MrWhite
    Jan 27, 2021 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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