Apache DirectorySlash redirects HTTPS requests back to HTTP

amazon-elbapache-2.2httphttpsredirect

User Request: https://www.example.com/test

HTTPS requests --> AWS ELB HTTPS Listener --> Apache HTTP

Apache getting http://www.example.com/test

Apache redirect it to http://www.example.com/test/ due to DirectorySlash is On by default.

User ends up with a HTTP request: http://www.example.com/test/

AWS provides a HEAD to detect origin request protocol: %{HTTP:X-Forwarded-Proto}, but how do I tell Apache mod_dir DirectorySlash to use that Header?

Please advise your solution or workaround in this scenario.

Best Answer

Since rewrite will kick in before DirectorySlash, here is what ended up with and it works:

# Redirect to HTTPS before Apache mod_dir DirectorySlash redirect to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteCond %{LA-U:REQUEST_FILENAME} -d
RewriteRule ^/(.*[^/])$ https://%{HTTP_HOST}/$1/ [R=301,L,QSA]