How to conditionally modify Apache response header 'Location'
Asked Answered
C

1

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

Couturier answered 6/8, 2014 at 10:49 Comment(0)
E
16

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

Empirical answered 6/8, 2014 at 11:3 Comment(3)
Many thanks - I was looking at an earlier spec for mod_headers - before edit was added - doh!Couturier
To keep the query string when specified, you can add this one : Header edit Location ^wrongserver.com(.*)$ rightserver.com/$1Felting
@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 demonstrates this kind of thing (they replace https: protocol with http:).Blomquist

© 2022 - 2024 — McMap. All rights reserved.