Mobile app version of vmapp.org
Login or Join
Murphy175

: Mod_rewrite to edit cookies conditionally 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.

@Murphy175

Posted in: #Cookie #ModRewrite

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, I hope it is okay.

RewriteRule s/.[0-9][0-9][0-9]/COOKIE=MSFNODE/g;



MSFNODE is a value in my cookie.
The thing I want to run my regex on is SESSIONID (also in my cookie). I'm not sure how to choose that as a target.
As the regex shows, I want to replace .[0-9][0-9][0-9] with my cookie value called MSFNODE, but 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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Murphy175

1 Comments

Sorted by latest first Latest Oldest Best

 

@Sue5673885

You can Get/Detect/check for existance of cookie through RewriteCond %{HTTP_COOKIE}

Set Cookie value with the RewriteRule flag cookie|CO which has this syntax:
[CO=NAME:VALUE:DOMAIN:lifetime:path:secure:httponly]

An example with them both together:

RewriteCond %{HTTP_COOKIE} cookiekey=cookievalue [NC]
RewriteRule ^(.*)$ / [L,CO=cookiekey:NewCookieValue:mydomain.com:86400:/]


In the example above the redirection will only happen if cookiekey has a value of cookievalue. You can also use regex in the cookievalue RewriteCond if you desire.

More examples (read user comments as well): www.askapache.com/htaccess/htaccess-fresh.html#Cookie_Manipulation_Tests_mod_rewrite

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme