API Redirect
A server triggers a redirection via sending special responses which
status code typically is 301
, 302
, 303
, 307
, and 308
.
You may want to redirect API requests in some cases, e.g., you're migrating
the API and the Host or URI are changed.
In this guide, you'll learn how to configure redirection rules on API7 Cloud with the help of the Redirect plugin.
The Redirect plugin doesn't support redirecting HTTP requests to HTTPS. If you want, see Build HTTPS Only Service for the details.
Prepare the Environment
Deploy Apache APISIX
Please refer to How to Deploy Apache APISIX to learn how to deploy Apache APISIX and connect it to API7 Cloud. In this guide, we'll deploy an Apache APISIX instance on Docker.
Create Service and Route
We'll create a service with the following details in this guide.
- The service name is
redirect-app
. - The path prefix is
/v1
. - The HTTP Host is
redirect.httpbin.org
. - The upstream URL is
https://httpbin.org
.
Besides, we'll create a route inside the redirect-app
Service.
- The route name is
anything
. - The path is
/anything
(prefix match), and strip the path prefix. - It accepts all HTTP methods.
If you don't know how to configure a service and route, please refer to the Getting Started guides first
Test Direct Redirection
One of the running modes of the Redirect plugin is redirecting your API requests to another URL. We call it the replace mode.
Now let's create the Redirect plugin for the anything
route.
We'll redirect all API requests to /v1/json
. Now let's send a request to verify it.
curl http://127.0.0.1:9080/v1/anything -H 'Host: redirect.httpbin.org' -i
HTTP/1.1 302 Moved Temporarily
Date: Wed, 10 Aug 2022 06:24:02 GMT
Content-Type: text/html
Content-Length: 142
Connection: keep-alive
Location: /v1/json
Server: APISIX/2.15.0
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>openresty</center>
</body>
</html>
As you can see, the Location header contains the new URL we want.
Test Regex Substitution
The Redirect plugin also has a fine-grain running mode, which constructs the Location header according to the current route path and a regex template.
Let's update the Redirect plugin.
In this case, we hope all API requests led by /v1
will be replaced with /v2
.
Let's send a request and see what will happen.
curl http://127.0.0.1:9080/v1/anything -H 'Host: redirect.httpbin.org' -i
HTTP/1.1 302 Moved Temporarily
Date: Wed, 10 Aug 2022 06:29:01 GMT
Content-Type: text/html
Content-Length: 142
Connection: keep-alive
Location: /v2/anything
Server: APISIX/2.15.0
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>openresty</center>
</body>
</html>
Let's focus on the Location header. It's /v2/anything
, which is the expected one.