redirect
Redirects requests to another URI or to HTTPS.
redirectPriority 900What it does
redirect answers a request itself instead of passing it on to your backend. The client receives a redirect status and a Location header naming the address to call instead. Its commonest job is HTTPS: with HTTP to HTTPS on, a request over plain HTTP is sent to the same host, path and query string over HTTPS, and a request that arrives over HTTPS passes untouched.
It knows only how the request reached the gateway. Behind a load balancer that ends HTTPS and sends the gateway plain HTTP, every request arrives that way. redirect then sends even a client that called over HTTPS back to HTTPS, again and again. There, send plain HTTP to HTTPS on the load balancer itself.
When to use it
- Clients that still call a route over plain HTTP, once the host answers HTTPS: they are sent to the HTTPS address.
- A path that moved: clients of the old path are sent to the new one,
/v2/ordersfor example. - A whole section of paths that moved, everything under
/docs/to/help/for example, rewritten by one pattern.
Not for changing the path your backend receives without the client noticing — see proxy-rewrite (Rewrites the request URI, method, host and headers before proxying).
Guides
Pages that walk through a job with redirect, step by step.
- Send HTTP clients to HTTPSAnswer plain-HTTP requests with a redirect to the HTTPS address.
Set it up
Send plain HTTP requests to HTTPS
Add redirect once `https://api.example.com` answers. A client that calls `http://api.example.com` is then sent there.
Choose Redirect to HTTPS and turn on HTTP to HTTPS.
{
"http_to_https": true
}
What you will see
- A request over plain HTTP to
api.example.comis answered301withLocation: https://api.example.com:9443/orders/3201?page=2: the same host, path and query string over HTTPS, on the gateway's own HTTPS port. - A POST over plain HTTP is answered
308instead, with the sameLocation. - A request that arrives over HTTPS passes untouched: over HTTPS to
api.example.comit reaches your backend and is answered200. - A header X-Forwarded-Proto set to https changes nothing: a request that reaches the gateway over plain HTTP is still answered
301. Behind a load balancer that ends HTTPS and sends the gateway plain HTTP, a client that already called over HTTPS is therefore sent to HTTPS again. - On a route that also carries key-auth, key-auth answers first: a request over plain HTTP without a key is answered
401with{"message":"Missing API key in request"}, not redirected, and one with a valid key is answered301.
More scenarios
Send clients to a new path permanently, keeping the method and the query string
Choose Redirect to a URI, set URI to /v2/orders, set Status code to 308 and turn on Append query string.
{
"uri": "/v2/orders",
"ret_code": 308,
"append_query_string": true
}
What you will see
- A POST to the old path with
?page=2is answered308withLocation: /v2/orders?page=2: the query string is kept.
Move a whole section of paths with one pattern
Choose Redirect by a pattern, fill Regex URI as in the JSON below and set Status code to 301.
{
"regex_uri": [
"^/docs/(.*)",
"/help/$1"
],
"ret_code": 301
}
What you will see
- A request for /docs/intro is answered
301withLocation: /help/intro.
Things to know
- HTTP to HTTPS — The client is sent to the same host, path and query string over HTTPS, on the gateway's own HTTPS port: a request for
/orders/3201?page=2atapi.example.comis sent tohttps://api.example.com:9443/orders/3201?page=2. - HTTP to HTTPS — A header
X-Forwarded-Protoset tohttpsby a load balancer changes nothing: the gateway goes by how the request reached it, so behind a load balancer that ends HTTPS and sends the gateway plain HTTP, every request is redirected. - Append query string — Turning it on beside HTTP to HTTPS is refused when you save: the HTTPS address already carries the query string.
- Status code — Only a 3xx moves the client: 200 answers with a Location header and an empty body, and 999 goes out as 999. With HTTP to HTTPS on, this is not read: GET and HEAD get 301, other methods 308.
- AdviceGive clients and their developers the
https://address from the start, and keep the redirect for those that still call plain HTTP. Add it to every route they may call that way. - HTTP to HTTPS — Goes by how the request reached the gateway: behind a load balancer that ends HTTPS and sends plain HTTP, every request is redirected again and again, whatever X-Forwarded-Proto says. Redirect on the load balancer there.
Settings
The fields as the console shows them, section by section.
Choose one:
Redirect to a URI
uristringRequiredThe address the client is sent to, such as /new-path or https://$host/new, where $name or ${name} takes that request variable. A name the request does not carry becomes empty. A backslash before the dollar stops the replacement but stays in the address: \$host is sent as \$host.
pattern
(\\\$[0-9a-zA-Z_]+)|\$\{([0-9a-zA-Z_]+)\}|\$([0-9a-zA-Z_]+)|(\$|[^$\\]+)/new-pathRedirect by a pattern
regex_uriarray of stringsRequiredBuilds the redirect target from the client's path: the first part is the pattern to match, the second the template of the new path.
Redirect to HTTPS
http_to_httpsbooleanRequiredret_codeintegerdefault 302append_query_stringbooleandefault falseencode_uribooleandefault false