Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
How do I preserve query parameters on a route without a trailing slash on Static Site hosted on S3 and Cloudfront

I have a statically generated site that I have uploaded to S3 and surface through a Cloudfront distribution - it has a root index.html file and several statically generated subpages (e.g. /donate/index.html). I have configured the bucket to act as a static host with index.html as an index document. And I can visit these pages by visiting the S3 prefix directly, e.g. /donate/ correctly displays the html in /donate/index.html.

Here's the issue - when I link to /donate?some=query&param=here S3 automatically 302 redirects these pages without a trailing slash to page with the trailing slash, removing the query parameters which are used by my client-side javascript for lightly tracking state.

For example /donate?some=query&param=here 302s to /donate/ and loses that context, which makes it hard for folks building URLs because they must remember to include the trailing slash (HTTP 302 is also a bad http response if you want web crawlers to remember the new route).

Here's what did not work:

  • Configuring Cloudfront to cache routes using query parameters in the distribution's caching policy (Cloudfront > Distribution > Behavior > Edit > Caching Policy)
  • Configuring Cloudfront to forward query parameters in the distribution's origin request policy (Cloudfront > Distribution > Behavior > Edit > Origin Request Policy)
  • Configuring the S3 bucket to redirect the routes using S3 redirection rules (S3 > Bucket > Properties > Static Web Hosting > Redirection rules)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's what finally cracked it - creating a Viewer Request Cloudfront Function to manually check if the path contains neither a file extension nor a trailing slash and adding one in those cases.

function handler (event) {
    var request = event.request;
    var uri = request.uri;

    if( !uri.includes('.') && !uri.endsWith('/') ) {
        request.uri = uri + '/'
    }

    return request;
};

Create this function in Cloudfront (Cloudfront > Functions > Create Function) and then attach it to your distribution's behavior (Cloudfront > Distribution > Behavior > Edit > Viewer Request > Cloudfront Functions).

Make sure you invalidate the site after the distribution has finished initializing with the new settings and test the new route via CURL - you should see the following behavior change:

# BEFORE:
curl -v --location http://example.com/donate?please=keep > /dev/null
> GET /donate?please=keep
> RESP 302
> GET /donate/
> RESP 200

# AFTER
curl -v --location http://example.com/donate?please=keep > /dev/null
> GET /donate?please=keep
> RESP 200

Is there another way? Will I regret paying for every Cloudfront function invocation? Is there a way to make this happen with purely Cloudfront or S3 policy changes?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!