Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

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

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 ^ - []
share|improve this answer

Your Answer


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.