Complex HTTP header modification using mod_rewrite

Oh, the strange things mod_rewrite does!

Complex HTTP header modification using mod_rewrite

Postby kwwall » Sat Jun 17, 2006 3:58 pm

I have a situation where 3 non-standard HTTP request headers will
always be present with each HTTP request and I need to
manipulate the value of the first, depending on the values of the
other two.

For explanatory purposes, let's call these header names
xxx, yyy, and zzz respectively.

Furthermore, header xxx has the form of a
comma-separated list of values (always non-empty), whereas
yyy is just a simple string with no commas. And the header value
for zzz is always either the string "true" or "false".

All three of these headers will be present with every HTTP request.

What I would like to do with mod_rewrite is to take the existing
HTTP request and modify the HTTP header xxx so that it appends
",<value_of_yyy_header>" and also appends ",super" if the header
value for header "zzz" has the value "true". An example will illustrate
this better:
Code: Select all
    xxx: aaa,bbb,ccc
    yyy: ddd
    zzz: true

I would like this HTTP request to be rewritten so that it is of the
Code: Select all
    xxx: aaa,bbb,ccc,ddd,super
    yyy: ddd
    zzz: true

In this example, had header "zzz" been "false", the first header
would have been:
Code: Select all
    xxx: aaa,bbb,ccc,ddd

(i.e., ",super" would not get appended).

The rest of the HTTP request must remain unaltered.

Can mod_rewrite do this, and if so, how? Since these HTTP requests
are being proxied to a J2EE application server, I could write a
J2EE servlet filter to handle do this at that end, but I'd prefer to just
have Apache do it via mod_rewrite (or mod_rewrite in combination
with other Apache modules). I've seen mod_rewrite do some
pretty amazing and complicated things though so, I'm betting that
this can be done. But I don't wish to spend more time trying to figure
out how to do it than it would for me to just write a J2EE servlet
filter to do this.

Any assistance would be greatly appreciated.
Thanks in advance,
P.S.- I'm a relative newbie to mod_rewrite, but understand REs very
well and the basic Apache directives fairly well. So complicated
REs probably aren't going to confuse me as much as just knowing
what mod_rewrite directive to use in the first place.
Posts: 1
Joined: Sat Jun 17, 2006 3:25 pm

Postby richardk » Sun Jun 18, 2006 6:42 am

This isn't an idiosyncrasy... anyway http headers can be accessed with variables, eg for some_header, %{HTTP:some_header}. But you can't really edit headers, you can just set environmental variables, and even then they have REDIRECT_ prepended.

Code: Select all
Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{HTTP:zzz} ^true$ [NC]
# build it here
RewriteCond %{HTTP:xxx},%{HTTP:yyy},super ^(.+)$
RewriteRule .* %{REQUEST_URI} [QSA,E=HTTP_xxx:%1,L]

RewriteCond %{HTTP:zzz} ^false$ [NC]
# build it here
RewriteCond %{HTTP:xxx},%{HTTP:yyy} ^(.+)$
RewriteRule .* %{REQUEST_URI} [QSA,E=HTTP_xxx:%1,L]
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 1 guest