Gossamer Forum
Home : General : Internet Technologies :

Rewrite Regex

Quote Reply
Rewrite Regex
A little help with the following Rewrite regex would be greatly appreciated. The regex pattern matches the value of a cookie named COOKIE. The cookie value consists of two(2) parts, delineated by a hyphen:

1. First Part: 9 digit number. For example, 123456789
2. Second Part: mixed string that can consist of alphanumerical, dot, @, and underscore characters. For example, it could be an email address like blah_blah@somedomain.com.

In example above, cookie value would equal 123456789-blah_blah@somedomain.com

The regex retrieves the First Part and the Second Part, and assigns to two(2) environmental variables. Here is what I'm using:

RewriteCond %{HTTP_COOKIE} (^|;\s)COOKIE=(\w+)-([\w\d\@\.]+)(;.*|$)
RewriteRule .* - [E=P1:%2]
RewriteCond %{HTTP_COOKIE} (^|;\s)COOKIE=(\w+)-([\w\d\@\.]+)(;.*|$)
RewriteRule .* - [E=P2:%3]

However, P1 and P2 are undefined. They are defined if I use:

RewriteCond %{HTTP_COOKIE} (^|;\s)COOKIE=(\w+)-(\w+)(;.*|$)
RewriteRule .* - [E=P1:%2]
RewriteCond %{HTTP_COOKIE} (^|;\s)COOKIE=(\w+)-(\w+)(;.*|$)
RewriteRule .* - [E=P2:%3]

...and when the Second Part consists only of Word characters. Yet however, the first Rewrite regex expressions (in the first Rewrite block) do work when tested in Perl. Ideas?

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Rewrite Regex In reply to
Hi,

Apache's regex isn't the same as perl regex, and I don't think you have \w inside character classes. Try using [0-9a-zA-Z_] instead of \w.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Rewrite Regex In reply to
Brilliant, it worked - thanks, Alex!

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln