Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda