Under Apache, using the ProxyPassReverse directive will do what you want. It will take the redirect from the backend server and replace the backend host name with the front end name. From the documentation:
This directive lets Apache httpd adjust the URL in the Location
, Content-Location
and URI
headers on HTTP redirect responses. This is essential when Apache httpd is used as a reverse proxy (or gateway) to avoid bypassing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.
They also have an example of how it is typically used in conjunction with adjusting cookie paths:
ProxyPass "/mirror/foo/" "http://backend.example.com/"
ProxyPassReverse "/mirror/foo/" "http://backend.example.com/"
ProxyPassReverseCookieDomain "backend.example.com" "public.example.com"
ProxyPassReverseCookiePath "/" "/mirror/foo/"
You may find that there are also absolute links in the HTML documents from the backend server. If that is the case, you can use the mod_proxy_html module to also replace those.
It sounds like that in this case you didn't develop the backend webapp yourself and it may not have settings for running behind a proxy server. However, if you can modify the code on the backend webapp, you can modify it to write its links and redirects using the front end host name.
The front end proxy server passes the host name to the backend using the X-Forwarded-For
header. You can change your server to use the value from this header rather than ServerName
so that it works seamlessly behind a proxy.