DEV Community

Cover image for S3 redirect to another domain and preserving Query String
Rajan for AWS Community Builders

Posted on

5 2

S3 redirect to another domain and preserving Query String

One of our clients has Microsites transferred to independent domains. The Microsite URL is like www.rewards.com/loyalty1 and it’s moved to www.loyalty1.com. The hosting of both the sites are in AWS S3 with Cloudfront.

This is permanent change, and we all know that it should be 301 permanent redirect. As you see in this post, we have to update the Redirect rules in S3. The client has configured the rules, but they have old campaigns with UTM values referring to the earlier domain. As per S3 redirect rules, the sites are redirecting to new domain, but it does not preserve the query strings.

I have noticed two issues,

Referring the AWS documentation, noticed an element we can add to the rules. Protocol, this can be http, https or none. This will force the redirects to the protocol configured. This element is missing in the rules, so one other round-trip.

The issue of losing the campaign query strings during the redirect is because of the element ReplaceKeyWith. There is an excellent discussion at stackoverflow. So use ReplaceKeyPrefixWith.

Finally, following is the rule we configured,

{
    {
        "Condition": {
            "KeyPrefixEquals": "loyalty1"
        },
        "Redirect": {
            "HostName": "loyalty1.com",
            "HttpRedirectCode": "301",
            "Protocol": "https",
            "ReplaceKeyPrefixWith": ""
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This is redirecting to the new domain, but still there is one issue. The condition check is case sensitive. I don’t see any regex references in the documentation for redirect rules. Anyone has an alternate solution for this?

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (1)

Collapse
 
generonquillo profile image
Gene Ronquillo

This should help - stackoverflow.com/a/70428718

Create a simple OTP system with AWS Serverless cover image

Create a simple OTP system with AWS Serverless

Implement a One Time Password (OTP) system with AWS Serverless services including Lambda, API Gateway, DynamoDB, Simple Email Service (SES), and Amplify Web Hosting using VueJS for the frontend.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay