DEV Community

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

Posted on

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?

Top comments (1)

Collapse
 
generonquillo profile image
Gene Ronquillo

This should help - stackoverflow.com/a/70428718