Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to use mod_rewrite to modify values in cookies based upon a condition? I am completely new to HTTP, but I have tried a thing or two. The only regex I know is Perl-style, my hopes are it is okay.

RewriteRule s/.[0-9][0-9][0-9]/COOKIE=MSFNODE/g;
  1. MSFNODE is a value in my cookie.
  2. The thing I want to run my regex on is SESSIONID (also in my cookie)...not sure how to choose that as a target.
  3. As the regex shows, I want to replace .[0-9][0-9][0-9] with my cookie value called MSFNODE. ONLY IF MSFNODE EXISTS!!

Is COOKIE=MSFNODE the right way to do this? Also, I only want this to happen if MSFNODE exists - as it is now will it just pass on through and not follow the rewrite rule if MSFNODE doesn't exist?

I feel like even my most basic regex is flawed here.

share|improve this question

1 Answer 1

If you have application code that's setting the cookie, then my recommendation is that you handle your logic there. Shoehorning Apache into handling your application logic is not good.

In any case, this is certainly possible. I'm having a hard time figuring out what you're looking to change (Is it JSESSIONID or MSFNODE? Are you trying to filter to three sequential digits? If you're only setting MSFNODE if it already exists, what are you accomplishing?), so I'll provide an example in general terms:

# If the client sent a cookie named "foo" with some
# arbitrary value...
RewriteCond %{HTTP_COOKIE} foo=([^;]+)
# Then, we'll set a new cookie to send back to the client.
RewriteRule ^ - [CO:bar:newcookievalue:.example.com]
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.