如何在Apache中使用mod_lua时添加类似于mod_rewrite的QSA标志的查询字符串?

我想编写一个脚本,在 mod_lua 中重写 URL,也就是说,我想继承 mod_rewrite 的 QSA 标志类似的查询字符串。

mod_rewrite 配置:

RewruteCond ^/foobar/([0-9A-Za-z]+)/(.+)$
RewriteRule ^.*$ /foobar/$2?_ID_=$1 [QSA,L]

我试着在 mod_lua 中编写以下代码,但它并不能很好地工作。请告诉我哪里出了问题?还有,它是否能够更简单?

mod_lua 配置:

LoadModule lua_module modules/mod_lua.so
LuaHookTranslateName /usr/local/httpd/conf/extra/lua/router.lua app_routing

/usr/local/httpd/conf/extra/lua/router.lua 路由:

require "apache2"
function app_routing(r)
  local matches = r:regex(r.uri, [[^/foobar/([0-9A-Za-z]+)/(.+)$]])
  if matches then
    r.uri  = "/foobar/" .. matches[2]
    if r.args then
        r.args = r.args .. "&_ID_=" .. matches[1]
    else
        r.args = "?_ID_=" .. matches[1]
    end
  end
end
点赞
用户5114817
用户5114817

在解决问题之前,需要注意以下几点:

  • r.args 不应该以"?"开头,问号不出现在查询字符串中,它是一个分隔符,用于标识查询字符串的起始位置,因此它实际上不是字符串的一部分。
  • 您应该为 mod_lua 和您的脚本启用调试。尝试在配置中添加“LogLevel lua:debug”,并检查错误日志的调试输出。此外,使用 r:info(logmessage_here) 向您的脚本添加一些调试信息,这将在错误日志中输出您自己的调试消息。

完成这些后,我们将有一个值得检查提示的错误日志。

2015-07-14 11:01:26