Forum Moderators: phranque

Message Too Old, No Replies

Can you Solve this htaccess riddle?

set a cookie in .htaccess based on condition

         

rankaboodle

7:19 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



How would I do the following all within the .htaccess file?

So I'm running Apache 1.3 and I have to do the following in .htaccess (I have to do this at the Apache level)

1. Check Cookie "name=checkme" in .htaccess

A) IF Cookie Not Set then do the following:

1a. Set a Cookie using .htaccess - "checkme=1"
2a. Redirect to an Image - "showme1.jpg"

B) ELSE Cookie is Set

1b. Redirect to an Image - "showme2.jpg"

I hope this makes sense. This would be easy in php for me to do but unfortunately I need to do this in .htaccess.

Any suggestions would be greatly appreciated!

rankaboodle

7:28 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



2nd part of the question:

OK IF the above is possible then this Step:

"2a. Redirect to an Image - "showme1.jpg""

I basically want to randomize the redirect so that:

50% of the time I redirect to "showme1.jpg"

* and *

50% of the time I redirect to "showme1.jpg"

Again all of this in the .htaccess file.

StoutFiles

7:41 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And why again MUST this be in .htaccess?

rankaboodle

7:58 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



I have a devious plan to deal with someone that basically stole me content and is hot linking to my images. I could basically basically just cut this person off of my images but if I can get this to work then this would be *better*

jdMorgan

8:29 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot set a cookie in Apache 1.3.x. .htaccess itself. This feature became available only in Apache 2.x (using mod_rewrite).

You can, however, use mod_headers directives within a <FilesMatch> container in .htaccess to set a cookie based upon a file load (any file or a specific file -- for example, your showme1.jpg image).

That cookie *can* be checked in Apache 1.3.x or 2.x .htaccess using a mod_rewrite RewriteCond.

If you are trying to flip back and forth between two images, then part of your logic is missing -- You'll need to clear the cookie when the second image is loaded. And if this has to work every time, then you must also mark both images as non-cacheable (also using mod_headers, plus mod_expires).

So, that's three bits of Apache module documentation, plus the Apache Core doc to get started with... :)

Jim

rankaboodle

5:49 pm on Jul 19, 2008 (gmt 0)

10+ Year Member



Thanks jdMorgan for you very valuable input. It helped a lot! Also thanks for StoutFiles for making me question whether I should do this all in .htaccess because in the end I got this to work with a combination of the 2 .htaccess and php.

Basically what I did is I checked for the cookie in the .htaccess file. If it was set then I would show the image. Otherwise I used a php script and redirect to set the cookie and show the second image.

jdMorgan - I noticed when I was purely using .htaccess to set a cookie using this:

<FilesMatch "^test2\.html$">
Header set set-cookie: "mobile; path=/; domain=MYDOMAIN.com"
</FilesMatch>

and then doing a rewrite rule after the <FilesMatch> to redirect... the cookie never got set. However when I just do the <FilesMatch> without the redirect the cookie gets set....do you know why this is so?

janharders

6:00 pm on Jul 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It might be (not sure!) because the rewrite rule comes first and FilesMatch won't match on the rewritten request..

but, btw, why don't you just check the referer? You could just say "if the referer isn't empty and it isn't from my site, then show them some picture telling them to go to my website and shun the hotlinker"

jdMorgan

8:39 pm on Jul 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Already posted in another thread, but helps answer the <FilesMatch>/cookie question:

It's critical to understand that directives in your .htaccess file *are not* executed in the order you type them -- You should not view .htaccess code as a sequentially-executed "program." Directives in .htaccess are executed on a per-module basis, as determined by the server configuration. That is, all directives handled by the first module are executed, followed by all directives from the second module, etc. So the order in which you put your code only affects the relative order of execution of directives handled by the same module.

Module execution order on Apache 1.x is determined by the reverse LoadModule order in httpd.conf. It is determined by an internal priority scheme on Apache 2.x.

Jim