Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Apache is a reverse-proxy to my app. A client requests http://cdn.example.com/foo/images/logo.png:

GET /foo/images/logo.png HTTP/1.1
Host: cdn.example.com

I want Apache to modify the request so that the app on the other side of the reverse proxy receives it in the format http://foo.example.com/images/logo.png:

GET /images/logo.png HTTP/1.1
Host: foo.example.com

Rewriting the URL is easy, but I haven't found a way to modify the Host header with a value extracted from the URL. Is this possible?

share|improve this question
up vote 2 down vote accepted

Yes, you can change headers based on URIs. It's ugly though -- This is the only way I know of to do it:

  1. Use SetEnvIF to set an environment variable if the URI matches what you want to rewrite.

  2. Use mod_headers' RequestHeader directive to reset the appropriate Request Header (Host:) if that environment variable is set.

share|improve this answer

Using the SetEnvIf and Header mentioned above, here is what I did to rewrite the "Accept-Encoding" header to reduce the caching copies created by mod_cache, tested working.

# rewrite variation of the Accept-Encoding header to the same one
# to reduce the caching copies
UnsetEnv compression_ok
SetEnvIfNoCase Accept-Encoding ".*gzip.*deflate.*" compression_ok=1
RequestHeader set Accept-Encoding "gzip,deflate" env=compression_ok

What this does: Different browser sets the Accept-Encoding slightly differently, like "gzip,deflate" vs "gzip, deflate" (with extra space), and these causes the mod_cache to create different copies of the content. By rewrite it to the same value, the mod_cache only generates one copy. (note: my server only care about "gzip,deflate", yours might be different).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.