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?


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.

  • You most probably want to configure a reverse proxy, not a redirect. Commented May 25, 2020 at 12:24
  • No, the site is running locally within the container. I have a proxy in front of the Apache, however, Traefik 1.7. But redirects generated by Apache all have the port included in them, which I do not want.
    – Tim Stoop
    Commented May 25, 2020 at 12:25
  • Mind showing us that part of your configuration where it goes wrong? Commented May 25, 2020 at 15:30

1 Answer 1


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.

  • Agreed in part. But I'm also trying to simply bend Apache to my will and I'm annoyed by the fact that that's so hard..It does not add the port when redirecting for port 80 or 443. Is there no way to replicate that behaviour?
    – Tim Stoop
    Commented May 25, 2020 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .