URL Redirection Using API Gateway
Adil Ansari
Posted on December 3, 2023
API gateway is a popular choice of many developers and cloud engineers for multiple use cases. However redirecting to a new URL using API Gateway can be challenging unless you give proper response headers to it.
This article is a quick demo of how to achieve URL redirection using API gateway and Lambda function.
Use Cases of URL Redirection:
- URL Shortening Services.
- Website Rebranding
- Affiliate Links
- etc.
What is URL Redirection
URL redirection is the technique to move the user to a new URL.
For example in a URL shortening service, when you click on a short URL it redirects you to the original URL.
Now let's start creating Lambda Function.
Creating Lambda Function
- To create a lambda function, follow first part of this article How to Schedule a Lambda Function
- In lambda function, return StatusCode:302 and below headers to redirect to redirct_url: "headers": {"Location": redirect_url} For Example:- ```
def lambda_handler(event, context):
# URL to which you want to redirect
redirect_url = "https://www.google.com"
response = {
"statusCode": 302,
"headers": {
"Location": redirect_url,
},
}
return response
Our Lambda Function is ready. Now let's create API Gateway (You can use your existing one also).
## Creating API Gateway
* Go to API Gateway Service in AWS and click on **Create API**.
* Select REST API or HTTP API (In this demo we are choosing REST API) and click on Build.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egfi4cg6d3g46e7us70f.png)
* Give your API a name and Create API.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oqov2levdo6utlct91p.png)
* Now create a resource and enable CORS also for testing.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhx10nsx1qanl823uqbc.png)
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp20zavcyqhb933ce6bx.png)
* Now create a method and choose _integration type_ as **Lambda Function** and don't forget to switch on the **lambda proxy integration** switch. Now select your lambda function and click on Create method.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkdzqdz25ygajphzbn2w.png)
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sngwh9mho3z98ntgniz3.png)
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bgeazpn9ss4pws50m11.png)
* Now Deploy the API by clicking on **Deploy API** button and select/create a stage.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7vz3k5ihlmm1ghls5bx.png)
* Now on the stages section, you will get the Invoke URL for your API. It would be in this format - https://_yourApiID.execute-api.AwsRegion_.amazonaws.com/_stage_
In my API, it is _https://5m78szju8k.execute-api.ap-south-1.amazonaws.com/dev_.
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bygl5vd7gme78s9psam.png)
So just copy this URL and add resource name at the last like this:
https://5m78szju8k.execute-api.ap-south-1.amazonaws.com/dev/demo and hit the URL.
* Hurrayyy!! Now you are redirected to your your desired page (Our case it is google.com).
## Conclusion
You can create GET/POST APIs to redirect to a different URL.
You can also pass the _redirect_url_ in your POST request body and use it later to redirect.
Or you can fetch this URL from somewhere else like Databases or from other services.
Use cases are unlimited it depends on you.
**Hope you liked this article. If you find it useful, Like and share this article with the ones who need it. And follow me and support my contributions.
[dev.to](https://dev.to/adilansari) [LinkedIn](https://www.linkedin.com/in/adilansari488/)
Posted on December 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.