如何像这样添加到 .htaccess 条件?

How add to .htaccess condition like this?

需要像这样添加到 .htaccess 条件:

if (page == '/admin') {
    Header add Test "test"
}

这不起作用:

<IfModule mod_headers.c>
    <If "%{REQUEST_URI} == '/'">
        Header add Test "test"
    </If>
</IfModule>

但这适用于所有页面(逻辑上):

<IfModule mod_headers.c>
    Header add Test "test"
</IfModule>

我知道 env=[!]varname,但不知道在我的情况下如何使用..

我很乐意提供任何帮助!

指令

<IfModule mod_headers.c>
    <If "%{REQUEST_URI} == '/admin'">
        Header add Test test
    </If>
</IfModule>

是正确的,header 将被添加,如果 请求没有被修改。


例如,如果您有

<IfModule mod_headers.c>
    <If "%{REQUEST_URI} == '/admin'">
        Header add Test test
    </If>
</IfModule>

RewriteEngine on
RewriteRule ^admin$ /index.php [L]

不会设置header,因为请求已修改,将被识别为/index.php。要在响应中设置 header,您必须将其更改为

<IfModule mod_headers.c>
    <If "%{REQUEST_URI} == '/index.php'">
        Header add Test test
    </If>
</IfModule>

RewriteEngine on
RewriteRule ^admin$ /index.php [L]

现在请求/admin将被重写为/index.php,并且REQUEST_URI被处理为“/index.php”并且header被设置为在将响应发送到客户端之前,请参阅 mod_headers - Early and Late Processing

The normal mode is late, when Request Headers are set immediately before running the content generator and Response Headers just as the response is sent down the wire. Always use Late mode in an operational server.


因此,根据请求在您的环境中的处理和修改方式,您还必须将条件 <If "%{REQUEST_URI} == '...'"> 修改为适合您的环境的任何内容。