Nicholas DiPiazza
Nicholas DiPiazza

Reputation: 10585

How to encode cookies set by mod_rewrite?

We have a scenario where we are using a rewrite rule condition in Apache 2.2's httpd.conf to take a parameter from the query string, and write it as a Cookie:

CustomLog "/www/apache/ndipiazza/logs/ssl_request_log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b \"%{Cookie}i\""

RewriteEngine On
RewriteCond %{QUERY_STRING} CID=(.*)
RewriteRule .* - [L,CO=COOKIE_CID:%1:ndipiazza-local.com:50000] 

So for example, The URL "http://ndipiazza-local/mypage.jsp?CID=NickDogg" Creates a cookie "COOKIE_CID = NickDogg"

However, we can't quite use this solution because we have a restriction that we cannot store this cookie as clear-text. As of now, it writes in the log as clear text, and it writes the cookie as clear text. We need to encode it.

Is there a way to make apache encode the cookie being stored?

Upvotes: 0

Views: 992

Answers (1)

Nicholas DiPiazza
Nicholas DiPiazza

Reputation: 10585

Yes there is a way to do this.

For this you want to use a RewriteMap with MapType "prg".

So you would define a RewriteMap like

RewriteEngine On 
RewriteMap encode-map prg:/path/to/encode_script 
RewriteCond %{QUERY_STRING} UID=(.*) 
RewriteRule .* - [L, CO=COOKIE_CID:${encode-map:%1}:ndipiazza-local.com:50000] 

The /path/to/encode_script is a script on your system that is started once when apache starts and then gets newline terminated strings to encode and has to output newline encoded strings WITHOUT stdout buffering.

For further information and examples look at section "External Rewriting Program" on http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html and the "prg" examples on http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html and http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide_advanced.html

Upvotes: 1

Related Questions