Apache redirect response header

Submitted by DenRaf on Fri, 08/24/2007 - 23:08

All admins of advanced Apache setups will cross this problem someday. So did I :)

You have some nice reverse proxy setup, everything working just fine, except that one redirect that happens on one of the backend machines to a non default port.
You have no choice then redirect that to a default port. I searched long after this, and with me a collegue

Lets say when you surf to http://myhost.mydomain.com, you are proxied to backend server with ip 193.155.88.23. End for example that backend server redirects you to a non-default port like 8443. And you end up with your browser going to 193.155.88.23:8443 directly. If these ports aren't blocked already, you'll probably go to somewhere completely different.
Normal redirects are catched by your proxy and you'll not see a thing, but not these.

Everybody knows the mod_rewrite module, but that only handles request headers. For this you need to change to response headers. You could do this with some fancy stuff with something self written for using mod_perl or so, but the solution is by far more simplier.

mod_headers is the answer to this problem:

In your VirtualHost config for that machine on your proxy:

Header edit Location ^https://193.155.88.23:8443 https://myhost.mydomain.com

This will have as a result that the location header send by your backend server will be changed to https://myhost.mydomain.com if it starts with https://193.155.88.23:8443. Your browser directly go to https://myhost.mydomain.com and problem is solved.