This might actually not be possible, but trying anyway: I want to create a container image for Apache without the need to run as root. So I set up the VirtualHost to listen to port 8080 instead of anything <1024. There's a proxy before Apache which directs the traffic for port 80 to port 8080 on the container. However, when I make Apache redirect, it always adds the port to the Location header. So the redirect send back is "Location: http://mysite:8080/index", which will fail as there's nothing listening on port 8080 publicly.
Is there any way of telling Apache to never add the port number to these redirects? I tried fiddling with UseCanonicalPhysicalPort, but that does not seem to have the desired effect. Is there any way to prevent it from ever adding that port number to anything?
EDIT
Ok, looks like this behaviour is hardcoded, when I look at the source code of mod_rewrite I notice it calls "ap_is_default_port" to determine whether it should add the port in the answer or not. And the only way to override it would be by overriding this line in the source, I think. If anyone knows of a better answer, I'm all ears.
Too long for a comment
IMHO You are trying to fix the wrong problem
The problem is not that Apache includes the port number it is listening on in the Location header (because for as far as Apache is concerned that is correct behavior, it is indeed listening on port 8080) but the problem is that your reverse proxy does not correct for that.
Normally a reverse proxy should be rewriting the location headers (and others) and change any reference to the internal URI’s the backend servers use to URI’s the clients need to use to reach the reverse proxy / load balancer.
Check your reverse proxy (treafik) config to find why that doesn’t happen.