0

I want to write a script that rewrite a URL in mod_lua, that is I want to inherit the query string like QSA flag of mod_rewrite.

mod_rewrite config:

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

I try to code in mod_lua as below, but it does not work well. Please tell me what is wrong? And, Does it will be able to be more simple code?

mod_lua config:

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 routing:

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
greatwolf
  • 20,287
  • 13
  • 71
  • 105

1 Answers1

0

A couple of things before we can really look into solving the issue:

  • r.args should never start with a "?", the question mark is not present in the query string, it is a delimiter that is used to denote where the query string begins, so it is not actually a part of the string itself.
  • You should enable debugging both for mod_lua and your script. Try adding "LogLevel lua:debug" to your configuration and check the error log for the debug output. Also, add some debugging to your script using r:info(logmessage_here), which will spit out your own debug messages in the error log.

Once this is done, we'll have an error log worth checking for hints.

Daniel Gruno
  • 114
  • 4
  • Thank you for replying. I will try to do 2 things you were advised. –  Jul 15 '15 at 00:18