htaccess-如何比较环境变量-检查两个环境变量是否相同

如果2个自定义环境变量的值不相同,则需要进行重定向。 在网络上搜索任何方法,或者StackOverflow都是令人沮丧的..已经花了几个小时,却发现没有任何东西可以做这个简单的事情。

到目前为止,这是我所拥有的,但是根本不起作用:

RewriteCond %{ENV:HREFBASE} !^$
RewriteCond %{ENV:PATHSTEM} !^%{ENV:HREFBASE}$
RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/%{ENV:HREFBASE}%{REQUEST_URI} [R=302,L]

明显错误的部分是:

%{ENV:PATHSTEM} !^%{ENV:HREFBASE}$

我已经尝试了几种方法,但是似乎没有任何效果。

我们将不胜感激并给予任何形式的回报;提前致谢。

更新

我什至尝试过:

RewriteCond %{ENV:HREFBASE} ^$
RewriteRule ^ - [L]

<If "%{ENV:PATHSTEM} != %{ENV:HREFBASE}">
    RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/%{ENV:HREFBASE}%{REQUEST_URI} [R=302,L]
</If>

I can see that the variables do not compare by catching a condition with PHP and printing out the entire $_SERVER array; the part that matters for this is:

[HREFBASE] => anon
[PATHSTEM] => Proc

-清楚地表明它们并不相同,但是重定向不会发生。

更新2

If a technical question seems too long (large/complex) -people run away, instead of reading, but, for the bold an fearless cyber-ninjas out there: below is an explanation followed by the entire .htaccess content.

关心

I've noticed that <if > blocks do not work as expected; the variables are empty, even tried the REDIRECT_ variants also .. When I run it with == operator it happens fine, which means inside the htaccess runtime inside the <if > block it's not present, at all.

分解

There is a PHP framework that handles initial requests for various reasons.
It is supposed to NOT interfere with another framework, like WordPress, -but handles initial requests up until an ALTHANDLER cookie is set.

When the cookie is found in the htaccess, or the request is for this framework directly, then it does not care about the requests anymore and let the other framework (like WordPress) handle it normally.

The framework runs in a subdomain, always, even if it is www -and a folder (directory) is made for any subdomain named as the subdomain name exactly.

Web根htaccess会根据请求处理重定向;因此,如果子域名存在一个文件夹,则规则应相应地处理所有这一切。

当Anon框架不再处理请求时,其余htaccess规则应仅检查请求路径的第一个“文件夹”的名称是否与子域完全相同,否则,应将其重定向为该子域并进行处理。

Here is the entire .htaccess content, it's not complete yet, but it does everything it's supposed to, except it does not redirect when Anon is done; I know this should be really simple, but ... maybe I'm just missing something:

# init :: flow : initialize rewrite engine and force subdomain as `www` .. this must be here 1st, at the top
# -----------------------------------------------------------------------------------------------------------------------------
    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
    RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# -----------------------------------------------------------------------------------------------------------------------------



# prep :: vars : this does no redirection and does not exit, it only writes variables to be used further down
# -----------------------------------------------------------------------------------------------------------------------------
    RewriteCond %{THE_REQUEST} \s/+([^?]*?)/*[\s?]
    RewriteRule ^ - [E=HREFPATH:%1]

    RewriteCond %{ENV:HREFPATH} !^$
    RewriteCond %{ENV:HREFPATH} ([a-zA-Z0-9-_\.]+)
    RewriteRule ^(.*)$ - [E=PATHSTEM:%1]

    RewriteRule ^(.*)$ - [E=ROOTPATH:%{DOCUMENT_ROOT}]

    RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
    RewriteRule ^(.*)$ - [E=HREFBASE:%1]
    RewriteCond %{ENV:ROOTPATH}/%{ENV:HREFBASE} !-d
    RewriteRule ^(.*)$ - [E=HREFBASE:]
    RewriteCond %{ENV:HREFBASE} ^$
    RewriteCond %{ENV:ROOTPATH}/%{HTTP_HOST} -d
    RewriteRule ^(.*)$ - [E=HREFBASE:%{HTTP_HOST}]

    RewriteRule ^(.*)$ - [E=ANONFOLD:.anon.dir]
    RewriteCond %{ENV:ROOTPATH}/%{ENV:ANONFOLD} !-d
    RewriteCond %{ENV:HREFBASE} !^$
    RewriteRule ^(.*)$ - [E=ANONFOLD:%{ENV:HREFBASE}/%{ENV:ANONFOLD}]
    RewriteCond %{ENV:ROOTPATH}/%{ENV:ANONFOLD} !-d
    RewriteRule ^(.*)$ - [E=ANONFOLD:]

    RewriteRule ^(.*)$ - [E=ANONINDX:.anon.php]
    RewriteCond %{ENV:ROOTPATH}/%{ENV:ANONINDX} !-f
    RewriteCond %{ENV:HREFBASE} !^$
    RewriteRule ^(.*)$ - [E=ANONINDX:%{ENV:HREFBASE}/%{ENV:ANONINDX}]
    RewriteCond %{ENV:ROOTPATH}/%{ENV:ANONINDX} !-f
    RewriteRule ^(.*)$ - [E=ANONINDX:]

    RewriteCond %{ENV:ANONFOLD} !^$
    RewriteRule ^(.*)$ - [E=DBUGPATH:%{ENV:ANONFOLD}/Proc/base/dbug.htm]
    RewriteRule ^(.*)$ - [E=BOTMATCH:bot|crawl|fetch|find|grab|scan|search|site|slurp|spider|wget|curl]
# -----------------------------------------------------------------------------------------------------------------------------



# cond :: Anon : for if Anon is installed AND: there is no Anon session, or request is for Anon directly
# -----------------------------------------------------------------------------------------------------------------------------
    RewriteCond %{ENV:ANONINDX} !^$
    RewriteCond %{HTTP_COOKIE} !ALTHANDLER=yes [OR]
    RewriteCond %{ENV:ROOTPATH}/%{ENV:ANONFOLD}/%{ENV:PATHSTEM} -d
    RewriteRule ^(.*)$ %{ENV:ANONINDX} [L,QSA]
# -----------------------------------------------------------------------------------------------------------------------------



# cond :: subdomain : if the request-path does not start with the existing-subdomain-folder-name then redirect it to be so
# -----------------------------------------------------------------------------------------------------------------------------
    <If "%{ENV:PATHSTEM} != '' && %{ENV:HREFBASE} != '' && %{ENV:PATHSTEM} != %{ENV:HREFBASE}">
        RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/%{ENV:HREFBASE}%{REQUEST_URI} [R=302,L]
    </If>
# -----------------------------------------------------------------------------------------------------------------------------

结论

如果请求路径中的第一个文件夹与子域名不同,我只需要按原样进行重定向,没有其他魔术,只需直接进行动态子域重定向即可;而已。

非常感谢您阅读所有这些内容;我希望我的努力不会白费;如果您能帮助我,我将确保您的努力会赢得声誉。