[Resolved] set cookie with rewrite rule

Status
Not open for further replies.
#1
hello,

i'm having an issue setting cookies with a rewrite rule in htaccess.

my intention is to check for the presence of a cookie named "mycookiename" on all html page requests, and if not present then set it:
Code:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !mycookiename
RewriteRule (.html) - [CO=mycookiename:mycookievalue:.example.com:0:/:0:1] [L]
the code apppears to work as intended as long as the url ends in .html. however it does not work when no cookie is set and the landing page is a directory (index.html) with the url ending in /

i expect that the solution is simply correct syntax, but i cannot find any solution by searching. at this point, i've tried so many variations, figured i'd save some time and ask for help!

thanks!
 
Last edited by a moderator:

Pong

Administrator
Staff member
#2
Try this
Code:
RewriteRule ^(.*\.html)?$ - [CO=mycookiename:mycookievalue:.example.com:0:/:0:1] [L]
to see if it helps
 
#3
pong, thank you for your help!

that's almost got it working as desired!

still an issue remains on direct navigation to a sub-directory:

-------------------

TEST CASE 1

direct nav in a fresh browsing session to:

example.com/sub-directory

server response: 301 > 200, resolves correctly, no cookie set... and though i have caching on dynamic pages disabled, on refresh > 200 still no cookie set

-------------------

TEST CASE 2

direct nav in a fresh browsing session to:

example.com/sub-directory/

resolves correctly, 200, no cookie set, and again, as with TEST CASE 1, on refresh 200 still no cookie set

-------------------

here's all code i have in htaccess which i think might be relevant:

Code:
RewriteEngine on

# remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

# set cookie on html page request if not yet set
RewriteCond %{HTTP_COOKIE} !mycookiename
RewriteRule ^(.*\.html)?$ - [CO=mycookiename:mycookievalue:.example.com:0:/] [L]
-------------------

again, I've tried everything I can think of.

help greatly appreciated!
 
Last edited:
#5
thanks pong, i tested removing it. first, after the code to remove the www, then removing the Last flag in both places. still no cookie being set on direct nav to a sub-directory... any other ideas?
 
Last edited:

NiteWave

Administrator
#6
tried following rewrite rule
Code:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !mycookiename
RewriteRule ^.*(/|\.html)?$ - [CO=mycookiename:mycookievalue:.example.com:0:/] [L]
within a
<VirtualHost ...>
</VirtualHost>
section in apache's httpd.conf, works in both litespeed and apache.

not sure if it meets your requirement.
 
#7
thank you nitewave.

this works, but i've found that it also sets cookies on requests for files other than .html. in a fresh browsing session if i request an image file directly, example.com/images/imagefile.png for example, a cookie is set.

in apache i'm using the following code which works perfectly:

Code:
<FilesMatch "\.(html)$">
  SetEnvIf Cookie "^.*mycookiename.*$" cookie_set=1
  Header set Set-Cookie: "mycookiename=mycookievalue; path=/;" env=!cookie_set
</FilesMatch>
for all html page requests (and only html page requests), a check is made for mycoookiename. if not present, it's set. i cannot seem to achieve the same objective in litespeed.

i'm open to suggestions! i really need to get this project completed! thank you for your help!
 
Last edited:

Pong

Administrator
Staff member
#8
Based on NiteWave's solution, within virtual host httpd.conf instead of .htaccess file, try a minor change to see if it helps.

RewriteRule ^(.*\.html)?$ - [CO=mycookiename:mycookievalue:.example.com:0:/] [L]
 

NiteWave

Administrator
#9
I did more local tests. now in document_root/.htaccess only:
RewriteEngine On
RewriteCOnd %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} \.html$
RewriteRule . - [CO=mycookiename:mycookievalue:.example.com:0:/]
this way is easier, and no need put rewriterules in httpd.conf's "<VirtualHost ...></VirtualHost>"
 
#10
pong:

thank you for your continued willingness to help! unfortunately, your last suggestion actually resulted in cookies not being set upon direct navigation to a sub-directory in a fresh browsing session, a problem i had previously. but again, very much appreciate your willingness to help!

nitewave:

thank you for your continued willingness to help as well! the code you last provided works almost perfectly in all cases tested. THANK YOU! there does remain one issue that i suspect will be easy to resolve: on direct navigation to the top level (home page) of a domain or sub-domain, no cookie is set.... suggestions?
 
#11
Thank you for the feedback.

I did tests again
Code:
RewriteRule . - [CO=mycookiename:mycookievalue:.example.com:0:/]
==>
Code:
RewriteRule .* - [CO=mycookiename:mycookievalue:.example.com:0:/]
i.e. add a *, resolved the issue if home page of a domain. can you have further tests, include homepage of sub domain ?
 
Status
Not open for further replies.
Top