How to conditionally modify Apache response header 'Location'

user3913979 picture user3913979 · Aug 6, 2014 · Viewed 26.4k times · Source

I need to be able to test the URL string in a Location response header for a certain pattern and if it matches replace it with another.

e.g. if response is Location: http://wrongserver.com I need to change it to Location: http://rightserver.com

Seems mod_setenvif only operates on request headers so I haven't been able to combine this with a "Header set" directive to achieve what I want.

Content is not being proxied so using mod_proxy directives doesn't seem to be an option.

Thanks, Bernie

Answer

arco444 picture arco444 · Aug 6, 2014

You should be able to do this with mod_headers

Header edit Location ^http://wrongserver.com$ http://rightserver.com

More info here: https://httpd.apache.org/docs/current/mod/mod_headers.html#header