Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda