httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alan Brown" <>
Subject [users@httpd] How To Set Cookies on Localhost Using RewriteRule Directive
Date Fri, 26 Mar 2010 18:32:04 GMT
Hi, I new to this mailing list and would like to ask for advice re the above 
problem. I am currently teaching myself web design and development, and 
currently working through various configurations of the Apache web server. 
(I am also working at the moment through the book on mod_rewrite by Rich 

I am running the Apache Friends XAMPP Package v1.7.2 on XP Pro SP3, which 
has Apache v2.2.12.

The problem is that with a directive like :-

RewriteRule /dir1/test\.html - [CO=testcookie:val123:localhost:1440, L]

the cookie is not being accepted by the browser, though Apache is actually 
sending it. I am testing with Firefox v3.6.2, and I can see that the correct 
SetCookie header is sent in the HTTP response using the Live HTTP Headers 
Firefox plug-in. But when I check in Firefox there is no cookie there.

I know that the immediate conclusion here is that this is not an Apache 
issue, because it is sending the cookie as requested in the RewriteRule 
directive. However I wonder if there may be some workaround to overcome this 
problem. I have done some investigation, and found that when using the PHP 
'setcookie' function, instead of using Apache to set the cookie, the 
following will work :-

setcookie('testcookie', val123, time()+60*60*24, '/dir1/');

But when I add in the domain parameter :-

setcookie('testcookie', val123, time()+60*60*24, '/dir1/', 'localhost');

then the cookie is not set in the browser.

If instead of 'localhost' as the domain I use an empty string, or if I use 
' ', ie. a single blank space, then the cookie IS set.

Because of that I have sought some workaround in Apache. Initially I thought 
I could somehow get Apache to accept the empty string or ' ' (single space) 
as the domain field in the CO flag of the RewriteRule directive. But, on 
trying :-

RewriteRule /dir1/test\.html - [CO=testcookie: :localhost:1440, L]

Apache throws up a syntax error in http.conf.

And on trying  :-

RewriteRule /dir1/test\.html - [CO=testcookie::localhost:1440, L]

Apache gets confused and assigns the domain to the value '1440'.

I tried a couple of other tricks :-

(1) Use a dummy empty string as a regex back-reference :-

RewriteRule /dir1/te()st\.html - [CO=testcookie:$1:localhost:1440, L]

(2) Use a dummy environment variable reference :-

SetEnvIf Request_URI . TESTVAR=

RewriteRule /dir1/test\.html - [CO=testcookie: 
%{ENV:TESTVAR}:localhost:1440, L]

But neither of these are helpful, though (2) looked like it may have worked 
if it had been possible to set TESTVAR to be a string consisting of a single 
blank space - however I can not see any way to do that.

I suspect the behaviour of the browser in rejecting this localhost cookie 
may be by design (see RFC 2965 section 3.3.2), and that all browsers will 
treat it in much the same way as Firefox.

Because I can find a workaround in PHP though, I am wondering whether there 
is any possible workaround for the Apache RewriteRule directive. I think 
this could be helpful to a lot of people who are testing applications and 
learning Apache and web development on a local machine, as they will 
encounter the same problem.

Any help/advice is much appreciated.

View raw message