Skip to main content
Version: 2.6




URI redirect.


http_to_httpsbooleanoptionalfalseWhen it is set to true and the request is HTTP, will be automatically redirected to HTTPS with 301 response code, and the URI will keep the same as client request.
uristringoptionalNew URL which can contain Nginx variable, eg: /test/index.html, $uri/index.html. You can refer to variables in a way similar to ${xxx} to avoid ambiguity, eg: ${uri}foo/index.html. If you just need the original $ character, add \ in front of it, like this one: /\$foo/index.html. If you refer to a variable name that does not exist, this will not produce an error, and it will be used as an empty string.
regex_uriarray[string]optionalUse 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_codeintegeroptional302[200, ...]Response code
encode_uribooleanoptionalfalseWhen set to true the uri in Location header will be encoded as per RFC3986

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

How To Enable#

Here's a mini example, enable the redirect plugin on the specified route:

curl  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/test/index.html",    "plugins": {        "redirect": {            "uri": "/test/default.html",            "ret_code": 301        }    },    "upstream": {        "type": "roundrobin",        "nodes": {            "": 1        }    }}'

And we can use any Nginx built-in variable in the new URI.

curl  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/test",    "plugins": {        "redirect": {            "uri": "$uri/index.html",            "ret_code": 301        }    },    "upstream": {        "type": "roundrobin",        "nodes": {            "": 1        }    }}'

Test Plugin#

Testing based on the above examples :

$ curl -iHTTP/1.1 301 Moved PermanentlyDate: Wed, 23 Oct 2019 13:48:23 GMTContent-Type: text/htmlContent-Length: 166Connection: keep-aliveLocation: /test/default.html

We can check the response code and the response header Location.

It shows that the redirect plugin is in effect.

Here is an example of redirect HTTP to HTTPS:

curl  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/hello",    "plugins": {        "redirect": {            "http_to_https": true        }    }}'

Disable Plugin#

When you want to disable the redirect plugin, it is very simple, you can delete the corresponding json configuration in the plugin configuration, no need to restart the service, it will take effect immediately :

curl  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/test/index.html",    "plugins": {},    "upstream": {        "type": "roundrobin",        "nodes": {            "": 1        }    }}'

The redirect plugin has been disabled now. It works for other plugins.