Apache redirect http to https with reverse proxy and matching context paths

In Apache 2.4.39, I am unable to get http to https redirection working when also reverse proxying and the context path matches.

For example, I want http://www.example.com/foo to redirect to https://www.example.com/foo when "ProxyPass /foo" is also present.

I have followed the advice here, which is quite standard based on googling: https://stackoverflow.com/a/21798882/4410356

So I basically have:

<VirtualHost *:80>
  ServerName www.example.com
  Redirect / https://www.example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName www.example.com
  # ... SSL configuration here ...
  ProxyPass               /foo http://localhost:9999/foo
  ProxyPassReverse        /foo http://localhost:9999/foo
</VirtualHost>

However, the redirect from http to https seems to only work if there is no matching ProxyPass path. For example:

http://example.com/foo => http://example.com/foo
http://example.com/bar => https://example.com/bar

The following Rewrite rules work, but Apache recommends against using Rewrite:

RewriteCond %{HTTPS} !=on
RewriteRule "^/?(.*)" https://galaxy.erdc.hpc.mil/$1 [R,L]

Apache docs also states:

In the case of the http-to-https redirection, the use of RewriteRule would be appropriate if you don't have access to the main server configuration file, and are obliged to perform this task in a .htaccess file instead.

but this is not the case for me (I have access to the configuration files).

Is it possible to get http redirection working in this setup without using Rewrite rules?


Try the same config but with a Location parameter to break out your ProxyPass config

<VirtualHost *:443>
  ServerName www.example.com
  # ... SSL configuration here ...
 <Location "/foo/">
   ProxyPass               http://localhost:9999/foo
   ProxyPassReverse        http://localhost:9999/foo
 </Location>
</VirtualHost>

First off, my apologies, it appears my question is invalid based on my actual setup (translating urls, names and sections).

My ProxyPass* directives were not within a VirtualHost block as the question shows, but were rather applied at a global level in ssl.conf above the default VirtualHost block.

By moving the ProxyPass directives into the default VirtualHost block, the HTTP -> HTTPS redirect now works as expected when the context path matches a ProxyPass path.