2016-05-31 111 views
0

这可能会也可能不会,但我认为我会问。Nginx - 重写多个参数

我们最近更改了一些查询参数,并希望301将旧版本重定向到新版本。

旧参数方案是categoryX=Y其中X是一些数量和Y为一些数量的(一个实例为category56=112)。新版本只是退出X,所以我们有category=112。现在,如果有已知数量的参数或单个参数,这似乎相当简单。但是,这些参数的数量可能会有所不同。例如:

http://www.example.com/some_directory/index.shtml?category56=112 
http://www.example.com/some_other_directory/index.shtml?category8=52&category11=2&other_param=12 

我猜没有办法基本上是“每一个”,通过这些参数,如果一个正则表达式匹配category(0-9+)=(0-9+)将其更改为category=(0-9+)

+0

做它在您的应用程式。这会容易得多。 –

+0

是的,我有点想(在应用程序中)。想要确保我没有失去明显的东西。 –

回答

0

您可以遍历您的参数,但您需要第三方Nginx Lua模块,它也是Openresty软件包的一部分。

有了到位,这些方针的东西应该做的工作......

location /foo { 
    rewrite_by_lua ' 
     local new_args = "exit" 
     local exit = false 
     local m, err = nil 
     -- load arguments 
     local args = ngx.req.get_uri_args() 

     -- loop through arguments and rebuild 
     for key, val in pairs(args) do 
      -- stop loop if "abort" arg is found 
      if key == "exit" then 
       exit = "true" 
       break 
      end 
      m, err = ngx.re.match(key, "category(0-9+)") 
      if m then 
       new_args = new_args .. "&category=" .. val 
      else 
       new_args = new_args .. "&" .. key .. "=" .. val 
      end 
     end 

     -- redirect if we have not been here before 
     if exit == "false" then 
      return ngx.redirect(ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.request_uri .. ngx.var.isargs .. new_args) 
     end 
    '; 
} 
+0

谢谢,不要以为我目前有这个模块可用,但会看看它,看看我们是否可以使用它。 –