Strange behavior of nested <If> directives and mod_rewrite

Pascal Christen <[email protected]>
Newsgroups gmane.comp.apache.user
Message-ID <CAF2YS120ucpd84ocp5Bt94kJdGta_yK0fYVTBizGd=Wz638-XQ@mail.gmail.com>
Hi

I have the following setup in my httpd.conf inside the <Directory
"/usr/local/apache2/htdocs">:
----
    RewriteEngine on
    RewriteRule ^nonexists.php$ /target.php?nonexists
    RewriteRule ^exists.php$ /target.php?exists

    <If "%{HTTP_COOKIE} != 'pear'" >
        Header always set x-request-first-if "YES"
        <If "%{HTTP_USER_AGENT} != 'apple'" >
            Header always set x-request-second-if "YES"
        </If>
        <Else>
            Header always set x-request-second-if "NO"
        </Else>
    </If>
----

The document root has the following files:
exists.php
target.php


Requesting these URL gives the following result:
GET nonexists.php:
x-request-first-if: YES

GET exists.php:
x-request-first-if: YES

GET target.php:
x-request-first-if: YES
x-request-second-if: YES

Expected result: Nested if also gets evaluated even for rewritten requests.


It even gets a little "spookier". Added a "dummy if" on the
REDIRECT_STATUS and now its working:
----
    RewriteEngine on
    RewriteRule ^nonexists.php$ /target.php?nonexists
    RewriteRule ^exists.php$ /target.php?exists

    <If "%{ENV:REDIRECT_STATUS} == 200">
    </If>
    <If "%{HTTP_COOKIE} != 'pear'" >
        Header always set x-request-first-if "YES"
        <If "%{HTTP_USER_AGENT} != 'apple'" >
            Header always set x-request-second-if "YES"
        </If>
        <Else>
            Header always set x-request-second-if "NO"
        </Else>
    </If>
----

GET nonexists.php:
x-request-first-if: YES
x-request-second-if: YES

GET exists.php:
x-request-first-if: YES
x-request-second-if: YES

GET target.php
x-request-first-if: YES
x-request-second-if: YES


Could someone explain to me what this behavior is exactly?

I get the same behavior for FreeBSD apache 2.4.62 and the httpd:2.4.62
docker image. I could easily provide my containerized test-setup if
needed.

Thanks for your help.
- Pascal
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.