【问题标题】:How to add a query string like QSA flag of mod_rewrite when using mod_lua in Apache?在 Apache 中使用 mod_lua 时如何添加查询字符串,如 mod_rewrite 的 QSA 标志?
【发布时间】:2017-01-30 08:36:01
【问题描述】:

我想写一个在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

【问题讨论】:

    标签: apache mod-rewrite lua mod-lua


    【解决方案1】:

    在我们真正研究解决问题之前需要做几件事:

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

    完成此操作后,我们将获得一个值得检查提示的错误日志。

    【讨论】:

    • 感谢您的回复。我会尽力做你被建议做的两件事。
    猜你喜欢
    • 2015-08-16
    • 2011-04-28
    • 2014-07-27
    • 2021-06-28
    • 1970-01-01
    • 2014-10-22
    • 2023-03-22
    • 1970-01-01
    • 2014-04-08
    相关资源
    最近更新 更多