13

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

user3913979
  • 131
  • 1
  • 1
  • 3

1 Answers1

15

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

arco444
  • 22,002
  • 12
  • 63
  • 67
  • Many thanks - I was looking at an earlier spec for mod_headers - before edit was added - doh! – user3913979 Aug 06 '14 at 11:17
  • To keep the query string when specified, you can add this one : Header edit Location ^http://wrongserver.com/(.*)$ http://rightserver.com/$1 – Pr Shadoko May 30 '19 at 14:43
  • 2
    @PrShadoko If you don't use `$`, then the regex only matches the hostname and the remainder of the URL should stay untouched. The only "edit" example in the [mod_headers documentation](https://httpd.apache.org/docs/current/mod/mod_headers.html#examples) demonstrates this kind of thing (they replace `https:` protocol with `http:`). – Christopher Schultz Dec 04 '19 at 18:23