0

I would like to add the trailing slash in general when it is absent in the request URL:

when user requests https://example.com/blog, it should be redirected to https://example.com/blog/

when user requests https://example.com/abc.jpg, it should be served as it is

When the server is Apache, it seems it is usually done by mod_rewrite. Unfortunately, our system include a CDN (AWS CloudFront) which causes problem when use mod_rewite: the request will be redirected to the origin server, and the URL in the browser changes to origin server's URL.

I also tried mod_alias's RedirectMatch. But it seems it is too difficult for regular expressions to accomplish complex match patterns (in fact, it needs regex not match pattern).

So how to add the trailing slash in general for an Apache + CDN architecture?

Edit:

Thanks to Michael, it turns out that whitelist the Host header in CloudFront solves my problem: the redirected URL remains the same as the old one (with slash).

  • 2
    There should be a simple way to configure mod_rewrite to do the right thing. Failing that, in the CloudFront Cache Behavior settings, if you whitelist the Host header for forwarding to the origin, your Apache server will see that hostname instead of its own in the incoming request, causing the generated redirects to be correct. Try that? – Michael - sqlbot Dec 20 '18 at 0:17
  • 1
    How are requests being sent to the CDN? "the request will be redirected to the origin server" - that is perhaps indicative of an incorrectly (or incomplete) configured proxy? "it needs regex not match pattern" - "regex" and "pattern" are often used interchangeably in this context to mean the same thing (ie. a regex). mod_rewrite enables you to use conditions as well - which might be required in this instance (but not necessarily because of a regex-limitation). – MrWhite Dec 20 '18 at 0:52
  • @Michael-sqlbot Whitelist Host header works! Thank you! – shintaroid Dec 21 '18 at 3:42
  • @shintaroid You should add that as an answer and accept it to remove it from the unanswered question queue. Thanks. – MrWhite Dec 21 '18 at 9:28
0

This answer does not solve the problem refer to the title. However, since the key problem is how to redirect to the right url, I accept the following answer. It is credited to Michael

The reason that it redirects to origin URL is due to CloudFront's header forwarding. If we whitelist the Host header, it will redirect to the same domain as user's request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.