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

174
Visualizações
Apache: Remove trailing slash without specifying hostname in Location

I am using a rewrite rule similar to those described in mod_rewrite: remove trailing slash (only one!). I have a need to tweak this but I'm not sure how to do it.

My website is hosted behind a CloudFront distribution. CloudFront is configured with the public-facing domain name (e.g. mywebsite.com). The origin is configured using the server's hostname (e.g. websiteinternal.com). When a request is sent to something like https://mywebsite.com/foo/, the browser is redirected to https://websiteinternal.com/foo. This is because when CloudFront forwards the request to the origin, it sets the Host header to https://websiteinternal.com. Then the rewrite rule sets the Location header to https://websiteinternal.com/foo in the 302 response.

I found Remove trailing slash in .htaccess for CloudFront use, but the solution hardcodes the hostname in the Location header. This doesn't work because I have multiple lower environments with different domain names.

(In case you're wondering why I don't forward the Host header to the origin, I can't do it in my case, for reasons outside of the scope of this question.)

How can I tweak the rewrite rule to only set the Location header to the desired path, without the hostname? I want to set it to /foo, not https://websiteinternal.com/foo. This should result in a request to https://mywebsite.com/foo/ being redirected to https://mywebsite.com/foo.

I have tried the following and minor variations, with no luck. The server never sends a 302 response.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(https?://[^/]+)/(.+[^/])/$ /$2 [R=302,L]
over 4 years ago · Santiago Trujillo
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