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

173
Views
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
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!