Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support appending query string in redirect #4298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions apisix/plugins/redirect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ local schema = {
}
},
http_to_https = {type = "boolean"},
encode_uri = {type = "boolean", default = false}
encode_uri = {type = "boolean", default = false},
append_query_string = {type = "boolean", default = false},
},
oneOf = {
{required = {"uri"}},
Expand Down Expand Up @@ -191,8 +192,8 @@ function _M.rewrite(conf, ctx)
return
end

local index = str_find(new_uri, "?")
if conf.encode_uri then
local index = str_find(new_uri, "?")
if index then
new_uri = core.utils.uri_safe_encode(str_sub(new_uri, 1, index-1)) ..
str_sub(new_uri, index)
Expand All @@ -201,6 +202,14 @@ function _M.rewrite(conf, ctx)
end
end

if conf.append_query_string and ctx.var.is_args == "?" then
if index then
new_uri = new_uri .. "&" .. (ctx.var.args or "")
else
new_uri = new_uri .. "?" .. (ctx.var.args or "")
end
end

core.response.set_header("Location", new_uri)
return ret_code
end
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/plugins/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ URI redirect.
| regex_uri | array[string] | optional | | | Use regular expression to match URL from client, when the match is successful, the URL template will be redirected to. If the match is not successful, the URL from the client will be forwarded to the upstream. Only one of `uri` and `regex_uri` can be exist. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL template that is redirected to. |
| ret_code | integer | optional | 302 | [200, ...] | Response code |
| encode_uri | boolean | optional | false | | When set to `true` the uri in `Location` header will be encoded as per [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) |
| append_query_string | boolean | optional | false | | When set to `true`, add the query string from the original request to the location header. If the configured `uri` / `regex_uri` already contains a query string, the query string from request will be appended to that after an `&`. Caution: don't use this if you've already handled the query string, e.g. with nginx variable $request_uri, to avoid duplicates. |

Only one of `http_to_https`, `uri` or `regex_uri` can be specified.

Expand Down
1 change: 1 addition & 0 deletions docs/zh/latest/plugins/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ URI 重定向插件。
| regex_uri | array[string] | 可选 | | | 转发到上游的新 `uri` 地址, 使用正则表达式匹配来自客户端的 `uri`,当匹配成功后使用模板替换发送重定向到客户端, 未匹配成功时将客户端请求的 `uri` 转发至上游。`uri` 和 `regex_uri` 不可以同时存在。例如:["^/iresty/(.*)/(.*)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的 `uri` 正则表达式,第二个元素代表匹配成功后发送重定向到客户端的 `uri` 模板。 |
| ret_code | integer | 可选 | 302 | [200, ...] | 请求响应码 |
| encode_uri | boolean | 可选 | false | | 当设置为 `true` 时,对返回的 `Location` header进行编码,编码格式参考 [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) |
| append_query_string | boolean | optional | false | | 当设置为`true`时,将请求url的query部分添加到Location里。如果在`uri`或`regex_uri`中配置了query, 那么请求的query会被追加在这个query后,以`&`分隔。 注意:如果已经处理了query,比如使用了nginx变量`$request_uri`,那么启用此功能会造成query重复 |

`http_to_https`,`uri` 或 `regex_uri` 三个中只能配置一个。

Expand Down
90 changes: 90 additions & 0 deletions t/plugin/redirect.t
Original file line number Diff line number Diff line change
Expand Up @@ -910,3 +910,93 @@ Location: /hello/with%20space
--- error_code: 301
--- no_error_log
[error]



=== TEST 37: add plugin with new uri: $uri (append_query_string = true)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"redirect": {
"uri": "$uri",
"ret_code": 302,
"append_query_string": true
}
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 38: redirect
--- request
GET /hello?name=json
--- response_headers
Location: /hello?name=json
--- error_code: 302
--- no_error_log
[error]



=== TEST 39: add plugin with new uri: $uri?type=string (append_query_string = true)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"redirect": {
"uri": "$uri?type=string",
"ret_code": 302,
"append_query_string": true
}
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 40: redirect
--- request
GET /hello?name=json
--- response_headers
Location: /hello?type=string&name=json
--- error_code: 302
--- no_error_log
[error]