DEV Community

Cover image for URL Redirection Using API Gateway
Adil Ansari for AWS Community Builders

Posted on

URL Redirection Using API Gateway

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
Enter fullscreen mode Exit fullscreen mode

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.

  • Give your API a name and Create API.

  • Now create a resource and enable CORS also for testing.

  • 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.

  • Now Deploy the API by clicking on Deploy API button and select/create a stage.

  • 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.

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 LinkedIn

Top comments (0)