title: How to rewrite Location response header in a proxy setup with Apache?
body: <p>I have a primary proxy which sends requests to a secondary proxy on which OpeenSSO is installed.</p>
  
  <p>If the OpenSSO agent determines that the user is not logged in, it raises a 302 redirect to the authentication server and provides the original (encoded) URL that the user requested as a GET parameter in the redirect location header.</p>
  
  <p>However, the URL in the GET variable is that of the internal (secondary) proxy server, not the original proxy server. Therefore, I would like to edit/rewrite the "Location" response header to give the correct URL.</p>
  
  <p>E.g.</p>
  
  <ol>
  <li><a href="http://a.com/hello/" rel="noreferrer">http://a.com/hello/</a> (Original requested URL)</li>
  <li><a href="http://a.com/hello2/" rel="noreferrer">http://a.com/hello2/</a> (Secondary proxy with OpenSSO agent)</li>
  <li><a href="http://auth.a.com/login/?orig_request=http://a.com/hello2/" rel="noreferrer" title="http://auth.a.com/login/?orig_request=http%3A%2F%2Fa.com%2Fhello2%2F">http://auth.a.com/login/?orig_request=http%3A%2F%2Fa.com%2Fhello2%2F</a> (302 redirect to auth server with requested URL of second proxy server encoded in GET variable)</li>
  <li><a href="http://auth.a.com/login/?orig_request=http://a.com/hello/" rel="noreferrer" title="http://auth.a.com/login/?orig_request=http%3A%2F%2Fa.com%2Fhello%2F">http://auth.a.com/login/?orig_request=http%3A%2F%2Fa.com%2Fhello%2F</a> (Encoded URL is rewritten to that of the original request)</li>
  </ol>
  
  <p>I have tried pretty much all combinations of headers and rewrites without luck so I'm thinking it may not be possible. The closest I got was this, but the mod_headers edit function does not parse environment variables.</p>
  
  <pre><code># On the primary proxy.
  RewriteEngine On
  RewriteRule ^/(.*)$ - [E=orig_request:$1,P]
  Header edit Location ^(http://auth\.a\.com/login/\?orig_request=).*$ "$1http%3A%2F%2Fa.com%2F%{orig_request}e"
  </code></pre>