SSI/mod_include:新的 ap_expr 语法无法按文档工作

问题描述 投票:0回答:1

我的网站使用 SSI(服务器端包含)来检查条件(使用

#if expr
)并根据条件是 true 或 false 返回不同的 HTML 代码。这曾经工作多年,直到最近,我的网站开始显示错误
[an error occurred while processing this directive]
。我发现
expr
语法在较新版本的 Apache httpd
中发生了变化,因此我假设我的托管提供商最近升级了 Apache httpd,导致旧语法被破坏。 Apache httpd 现在版本为 2.4.29。

我设法通过将

SSILegacyExprParser on
添加到我的
.htaccess
文件中以返回到旧语法来暂时解决问题(感谢 这个问题)。但我想使用新语法找到永久解决方案。

这是与

SSILegacyExprParser on
一起使用的旧语法 SSI 代码:

<!--#if expr="${thisDoc} = /impressum\.shtml/" -->
<li id="current">
<!--#else -->
<li>
<!--#endif -->

(变量

thisDoc
已提前设置。)

这是使用新的

ap_expr
语法的 SSI 代码,但不起作用。再次显示
[an error occurred while processing this directive]
。在编写此代码时,我遵循了 http://httpd.apache.org/docs/current/expr.html 上的文档:

<!--#if expr="%{thisDoc} =~ /impressum\.shtml/" -->
<li id="current">
<!--#else -->
<li>
<!--#endif -->

我尝试以各种方式修改语法,例如避免使用正则表达式匹配并使用精确的字符串比较,但这也不起作用。我无法让单个表达式在新语法中工作。

为什么新语法不起作用?

apache ssi mod-include
1个回答
0
投票

我发现在较新的版本中我需要使用

env
函数来访问环境变量。

因此

${thisDoc}
变为
env('thisDoc')
env("thisDoc")

经测试可在 Fedora 37 上使用 Apache httpd 2.4.56:

<!--#if expr="env('thisDoc') =~ /impressum\.shtml/" -->
<li id="current">
<!--#else -->
<li>
<!--#endif -->
© www.soinside.com 2019 - 2024. All rights reserved.