Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite problem with cookies

mod_rewrite problem with cookies

         

kstorm001

1:08 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



I'm having a problem with mod_rewrite and cookies. We use cookies to keep track of our users' shopping carts, but there is a problem when using the mod_rewrite pages.

If we go to our homepage we get a cookie written as so:

username@domain
www.domain.com/ (written inside the cookie file)

But if I go directly to a page that uses mod_rewrite it looks like this:

page: www.domain.com/category/1234

cookie: username@category
www.domain.com/category (written inside the cookie)

Then when I click to go to an item: www.domain.com/item/1

It creates yet another cookie: username@item - www.domain.com/item]

Does anyone know why the cookie is not just being written as the domain?

Here are my rewrite rules:

VirtualHost *:80>
Servername www.domain.com
ServerAlias domain.com
RewriteEngine on
RewriteRule ^/item/([^/\.]+)/?$ /listitem.jsp?item=$1 [PT]
RewriteRule ^/item/([^/\.]+)/ad/([^/\.]+)/?$ /listitem.jsp?item=$1&ad=$2 [PT]
RewriteRule ^/category/([^/\.]+)/?$ /listcategories.jsp?c=$1 [PT]
RewriteRule ^/category/([^/\.]+)/ad/([^/\.]+)/?$ /listcategories.jsp?c=$1&ad=$2 [PT]
RewriteLog logs/rewrite.log
RewriteLogLevel 0
DocumentRoot /var/tomcat5/webapps/domain
JkMount /* loadbalancer
</VirtualHost>

I've looked all over, but haven't found anyone else with this problem. Any help would be greatly appreciated.

jdMorgan

2:01 pm on Sep 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you use to write the cookie determines the cookie's "domain" or "path" which is not the same thing as the "domain name," but rather, the "domain" in which the cookie will be used.

A cookie may be set for an entire site, or for only part of the URL-space within that site. Change your code to set the cookie for your entire site, if that's what you want to do.

See the original cookie specification at [wp.netscape.com...]

Jim

kstorm001

3:00 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



I use jsp to write the cookie.

Cookie cookie = new Cookie("ShoppingCart", cartId);
cookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cookie);

I tried cookie.setDomain(".domain.com").
Do I need to set the path?

On a side note if i go to www.domain.com/listcategories.jsp?c=1

the cookie is set username@domain

but if i go to www.domain.com/category/1

the cookie is set username@category

kstorm001

3:13 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



Thanks for you help. I think I fixed the problem by adding

cookie.setPath("/");