Thursday, February 3, 2011

how to use a rewrite rule to test the date in a cookie?

I found out that you cannot use a RewriteCond backreference as a pattern condition in the next Rewrite Cond...

UPDATE: As it turns out, mod_rewrite doesn't expand any variables in the test pattern, so I am effectively only testing for the existence of the cookie... Any ideas on how to actually test the value instead of just the existence?

RewriteCond %{HTTP_COOKIE} niftyExpire=([^;\ ]+)
RewriteRule .*index.html /fake.html [E=niftyExpire:%1,C]
RewriteCond %{TIME} <{ENV:niftyExpire}
RewriteRule .*fake.html /pass.html [R,L]
RewriteRule .*index.html /fail.html [R,L]

Thanks,

-Eric

  • I'd comment instead if I had the rep...

    I don't think you need the backslash you have in your examples. E.g.

    RewriteCond %{HTTP_COOKIE} niftyExpire\=([^; ]+) [NC]
    

    should just be

    RewriteCond %{HTTP_COOKIE} niftyExpire=([^; ]+) [NC]
    

    There a great resource here with some examples (not specifically about dates, but including rewrite rules based on a cookie's value):

    http://www.askapache.com/htaccess/htaccess-fresh.html

    Eric : Thanks, I had found that, unfortunately it's not working for me.... right now, I have: RewriteCond %{HTTP_COOKIE} niftyExpire\=([^; ]+) [NC] RewriteCond %{TIME} < %1 [NC] RewriteRule ^.*$ /passed.html [R=302,L,NC] which is outputting this in the error log: [Thu Jul 08 17:39:30 2010] [alert] [client 189.58.20.117] /home/niftypre/public_html/.htaccess: RewriteCond: bad flag delimiters [Thu Jul 08 17:39:30 2010] [alert] [client 189.58.20.117] /home/niftypre/public_html/.htaccess: RewriteCond: bad flag delimiters

0 comments:

Post a Comment