Forum Moderators: phranque

Message Too Old, No Replies

Set cookie with htaccess

how do I do?

         

majjk

12:08 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



I believe it is possible to set a cookie with htaccess, but I've spent lots of time searching for ways to do this... no joy so far.

What I'd like to do is to do a 301 redirect for domain.com/index.php?ref=XYZ to domain.com, and set a cookie containing the info "XYZ".

The reason for this is that I have affiliates linking to me (therefore I need to track the "ref" bit...), and I'd like to keep all the "link juice" that comes with their links.

I'm sure this is easy, but unfortunately htaccess isn't my strongest skill. Ideas on how to achieve this?

jdMorgan

3:09 pm on Jul 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This function is only available in Apache 2.x, not in Apache 1.x. For Apache 2.x, see the "set cookie" flag for RewriteRule.

If you're on Apache 1.x, you can use Apache mod_headers to set the cookie, and a <FilesMatch> container to make it conditional upon a particular filename. Notice that I said filename, not URL, so be careful here.

Seesion Cookie example:


<FilesMatch "^mobileset\.html$">
Header set Set-Cookie: "mobile; path=/; domain=example.com"
</FilesMatch>

If you need to set a specific-duration cookie instead of a session cookie, then you will need to use a script to do it, since there is no facility in .htaccess to calculate time or to convert time formats.

You can test cookies in mod_rewrite on Apache 1.3.x and later.

Jim

[edited by: jdMorgan at 3:10 pm (utc) on July 9, 2008]

majjk

10:37 pm on Jul 10, 2008 (gmt 0)

10+ Year Member



I'm on Apache 1.3.x and do need a specific-duration cookie, so maybe I need to change my approach...

Can I maybe do a rewrite so that it appears that any requested page on my domain with "?ref=XYZ" is redirected to the same page, but without "?ref=XYZ", flag 301, but still serving the page with the variable (ref=XYZ) intact for my script to use? If this variable is available to my script, then I can always set the cookie through my script.

Is this possible, or have I misunderstood something?

If possible, how would I do that?

majjk

10:37 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



I have misunderstood... cannot be done like that.

I don't have access to mod_headers either, so am in a tricky situation. I'm gonna do some more thinking on this. Meanwhile, if anyone has any briliant ideas, don't hesitate...

jdMorgan

10:44 pm on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could set the cookie and do the redirect from within your script. I did not give this answer initially, because of the title you chose for this thread.

Jim

majjk

1:11 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



I don't know why I had this idea I had to do it through htaccess...

It seems this works just fine:

if (isset($_GET['ref'])){
setcookie ($c_name,$c_value,$c_expire,$c_path,$c_domain);
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com".$_SERVER['PHP_SELF']);
}

Thanks a lot for pointing this out to me.

[edited by: jdMorgan at 3:04 pm (utc) on July 12, 2008]
[edit reason] example.com [/edit]