【问题标题】:Apache mod_rewrite [E] Redirect prefix with RewriteCondApache mod_rewrite [E] 带有 RewriteCond 的重定向前缀
【发布时间】:2021-07-22 22:54:50
【问题描述】:

在 Apache 服务器中,如果匹配特定 URL,我需要禁用 keepalive。
为此,我使用 mod_rewrite 对其进行设置,然后让脚本运行。

RewriteCond %{REQUEST_URI} ^/specific_url
RewriteRule  ^ - [E=nokeepalive]
.......
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

现在我的问题是 nokeepalive 如果匹配,最终会被前缀/重命名为 REDIRECT_
从这个问题:When setting environment variables in Apache RewriteRule directives, what causes the variable name to be prefixed with "REDIRECT_"?
我认为这是由于RewriteRule
但它没有前缀 如果我删除 RewriteCond (这将禁用 keepAlive globaly 与任何 URL)
有没有办法用 RewriteCond 防止这个前缀?
我不能使用SetEnvIf,因为我使用mod_rewrite,所以我必须使用RewriteRule

SetEnv/SetEnviF 也不起作用,因为它们无法读取未使用 SetEnv/SetEnvif 分配的变量。 我尝试使用 SetEnvIf 仅在 REDIRECT_nokeepalive 存在时进行定义(因为无论值如何,都必须定义 nokeepalive) - 但无济于事。

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    我不能使用 SetEnvIf,因为我使用了 mod_rewrite,所以我必须使用 RewriteRule。

    您可以将 mod_setenvif 与 <If> 一起使用。

    <If "%{REQUEST_URI} =~ /regextomatchurl/">
    # or use <If "%{REQUEST_URI} == 'urlstring'">
    SetEnv nokeepalive 1
    </If>
    

    您可以删除该重写规则。

    或:

    你可以添加这个,它可能有效,未经测试:

    SetEnv nokeepalive ${REDIRECT_nokeepalive}
    

    编辑:

    记得在SetEnv 之前添加PassEnv REDIRECT_nokeepalive,因为SetEnv 无法从mod_rewrite 读取环境变量。

    只是指出:

    这里有两个空格,可能会导致其他 RewriteRule 出现问题:

    RewriteRule  ^ - [E=nokeepalive]
               ^^
    

    【讨论】:

    • 我在第一次设置后使用了第二种方法。这很丑陋,但它有效,我现在没有发现其他解决方法来解决 Apache 的这种行为。
    • SetEnv nokeepalive ${REDIRECT_nokeepalive} 将不起作用,因为 SetEnv 无法从未使用 SetEnv 或 SetEnvif 分配的变量中读取。我尝试使用 SetEnvIf 来定义仅当 REDIRECT_nokeepalive 存在时(因为 nokeepalive 只需要定义,无论值如何) - 但无济于事。
    • @R.Damasinoro 你可以在 SetEnv 之前使用PassEnv REDIRECT_nokeepalive
    • @R.Damasinoro,您可能还需要将${REDIRECT_nokeepalive} 更改为%{REDIRECT_nokeepalive}e
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2013-05-26
    相关资源
    最近更新 更多