redirect

Description

The redirect Plugin can be used to configure redirects.

Attributes

NameTypeRequiredDefaultValid valuesDescription
http_to_httpsbooleanFalsefalseWhen set to true and the request is HTTP, it will be redirected to HTTPS with the same URI with a 301 status code. Note the querystring from the raw URI will also be contained in the Location header.
uristringFalseURI to redirect to. Can contain Nginx variables. For example, /test/index.html, $uri/index.html, ${uri}/index.html, https://example.com/foo/bar. If you refer to a variable name that doesn’t exist, instead of throwing an error, it will treat it as an empty variable.
regex_uriarray[string]FalseMatch the URL from client with a regular expression and redirect. If it doesn’t match, the request will be forwarded to the Upstream. Only either of uri or regex_uri can be used at a time. For example, [“ ^/iresty/(.)/(.)/(.*)”, “/$1-$2-$3”], where the first element is the regular expression to match and the second element is the URI to redirect to. APISIX only support one regex_uri currently, so the length of the regex_uri array is 2.
ret_codeintegerFalse302[200, …]HTTP response code.
encode_uribooleanFalsefalseWhen set to true the URI in the Location header will be encoded as per RFC3986.
append_query_stringbooleanFalsefalseWhen set to true, adds the query string from the original request to the Location header. If the configured uri or regex_uri already contains a query string, the query string from the request will be appended to it with an &. Do not use this if you have already handled the query string (for example, with an Nginx variable $request_uri) to avoid duplicates.
redirect - 图1note
  • Only one of http_to_https, uri and regex_uri can be configured.
  • Only one of http_to_https and append_query_string can be configured.
  • When enabling http_to_https, the ports in the redirect URL will pick a value in the following order (in descending order of priority)
    • Read plugin_attr.redirect.https_port from the configuration file (conf/config.yaml).
    • If apisix.ssl is enabled, read apisix.ssl.listen and select a port randomly from it.
    • Use 443 as the default https port.

Enable Plugin

The example below shows how you can enable the redirect Plugin on a specific Route:

redirect - 图2note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "uri": "/test/index.html",
  4. "plugins": {
  5. "redirect": {
  6. "uri": "/test/default.html",
  7. "ret_code": 301
  8. }
  9. },
  10. "upstream": {
  11. "type": "roundrobin",
  12. "nodes": {
  13. "127.0.0.1:80": 1
  14. }
  15. }
  16. }'

You can also use any built-in Nginx variables in the new URI:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "uri": "/test",
  4. "plugins": {
  5. "redirect": {
  6. "uri": "$uri/index.html",
  7. "ret_code": 301
  8. }
  9. },
  10. "upstream": {
  11. "type": "roundrobin",
  12. "nodes": {
  13. "127.0.0.1:80": 1
  14. }
  15. }
  16. }'

Example usage

First, we configure the Plugin as mentioned above. We can then make a request and it will be redirected as shown below:

  1. curl http://127.0.0.1:9080/test/index.html -i
  1. HTTP/1.1 301 Moved Permanently
  2. Date: Wed, 23 Oct 2019 13:48:23 GMT
  3. Content-Type: text/html
  4. Content-Length: 166
  5. Connection: keep-alive
  6. Location: /test/default.html
  7. ...

The response shows the response code and the Location header implying that the Plugin is in effect.

The example below shows how you can redirect HTTP to HTTPS:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "plugins": {
  5. "redirect": {
  6. "http_to_https": true
  7. }
  8. }
  9. }'

To test this:

  1. curl http://127.0.0.1:9080/hello -i
  1. HTTP/1.1 301 Moved Permanently
  2. ...
  3. Location: https://127.0.0.1:9443/hello
  4. ...

Delete Plugin

To remove the redirect Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "uri": "/test/index.html",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:80": 1
  9. }
  10. }
  11. }'