...

View Full Version : What do I add to this code to redirect based on 2 cookie values?



thewiizard
05-08-2010, 02:39 PM
Hello,

I'm trying to redirect based on 2 separate cookies.

Example: If cookie1=true and cookie2=true then redirect

What would I need to change with the code below to get it to do that?




RewriteEngine on
RewriteCond %{HTTP_COOKIE1} cookie1=true
RewriteCond %{HTTP_COOKIE2} cookie2=true
RewriteRule .* /page3.html



Thanks

Samhain13
05-08-2010, 10:15 PM
Try this?

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_COOKIE} cookie1=true [NC]
RewriteCond %{HTTP_COOKIE} cookie2=true [NC]
RewriteRule .* page3.html

[NC] for making the condition case-insensitive.

Check this out: http://www.askapache.com/htaccess/htaccess-fresh.html#modrewrite2

thewiizard
05-09-2010, 08:46 AM
Thanks, but still no luck.

If I add a "!" in front of the cookie re-directs. But it redirects whether the cookie is set or not. If it's not there it doesn't do anything...



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !access1!=true [NC]
RewriteCond %{HTTP_COOKIE} !access2=true [NC]
RewriteRule .* page2.html


I should clarify that I want it to redirect only if access1 does not = true and access 2 = true.

I'm not sure if != is how it's done in .htaccess? If not, what is the equivalent of "does not equal"?

Thanks

Samhain13
05-09-2010, 03:17 PM
# access1 cookie is not true.
RewriteCond %{HTTP_COOKIE} !access1=true [NC]
# access2 cookie is true.
RewriteCond %{HTTP_COOKIE} access2=true [NC]

Don't put `!=` in the condition. Just a `!` before the cookie name to indicate "not".

thewiizard
05-09-2010, 04:04 PM
Thanks. I couldn't find the != equivalent anywhere.

I can get the re-direct to work, only when I use a does not equal. I can't get it to work with the = though. Here's the code..., do you see anything wrong with it?

(note: The cookie values from access1 and access2 either exist and are set to "true", or don't exist at all.)



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !access1=true [NC]
RewriteCond %{HTTP_COOKIE} access2=true [NC]
RewriteRule .* page2.html

Samhain13
05-09-2010, 05:39 PM
If the access1 cookie doesn't exist, "RewriteCond %{HTTP_COOKIE} !access1=true [NC]" will still be True, right? And it will be the same as if access1 were "false". May you can look at this in another way to lessen the confusion.

Test for True for both conditions:
access1=false
access2=true

This way, if access1 doesn't exist, the condition will return False. If access1 exists but the value is anything other than "false" (i.e., "true"), the condition will still return False. The only time the condition will return True is when the access1 exists and it has a value "false". :D

thewiizard
05-10-2010, 08:17 AM
Well, I've thoroughly confused myself ;-)

Okay, so I gave it a go. Something still isn't working.

So I got rid of the first condition, just to see if I could get it to redirect based on one cookie = true. It's not working. It does redirect on the condition, one cookie does not = true.

I've checked with cookies, and without.

Here's the code...



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} access2=true [NC]
RewriteRule .* page2.html


It does not redirect whether the cookie is set or not. The cookie is set to = the text value of "true" (not sure if that matters or not).

If I set the code to..



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !access2=true [NC]
RewriteRule .* page2.html


Then it redirects (whether the cookie is set or not).

If I set the cookie to not equal false then it redirects (whether the cookie is set or not).



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !access2=false [NC]
RewriteRule .* page2.html


If I set the code to = "true" it won't redirect... (whether the cookie is set or not)



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_COOKIE} access2=true [NC]
RewriteRule .* page2.html



It's almost like it's simply not reading the cookies period. The cookie is set another website (if that matters). The cookie is set so that any website can access it. I've double checked on this.

Any ideas...


Thanks

Samhain13
05-10-2010, 08:45 PM
Strange, I just tested this and it works fine.


RewriteEngine on
RewriteCond %{HTTP_COOKIE} access1=false [NC]
RewriteCond %{HTTP_COOKIE} access2=true [NC]
RewriteRule .* page2.html [L]

Granting that access2 exists and is set to "true":
1. If access1 has a "false" value, the page redirects.
2. If access1 does not exist, the page does not redirect.
3. If access1 exists but has a value other than "false" (like "true" or "albert"), the page does not redirect.

If access2 is not set to "true" (of if it doesn't exist), the page does not redirect no matter whether access1 exists and if it has the correct value.

----

I don't know how you're setting your cookies. Are they in the right domain and do they have the right path?

I tried this by setting cookies using the WebDeveloper extension to Firefox. One thing that bugged me was that when I set my cookies (access1 and access2) they expired immediately. I had to adjust the "expires" field so the cookies expire one year after being set. Just saying because we might be testing this using the same method. :D

thewiizard
05-13-2010, 09:42 AM
hmmm...

Yes, I think the problem lies in the way I'm setting cookies.

I'm setting them on a separate domain then the domain with the .htaccess redirect.

Path is set to "/"

The cookies are set to be accessible by third party websites...

I'm not sure if they need to be called differently in .htaccess IF they cookie was not set at the same site as the .htaccess file?

If it does need to be called differently, what would the code be for the redirect IF the cookies were set on another domain?

thewiizard
05-13-2010, 07:10 PM
Okay, I just discovered the problem...

Cookies are domain specific...Pretty obvious I'm sure. So I'm new to this, cut me some slack okay.

So what's the most elegant way to solve this problem...?

I simply want to redirect visitors from one site to the other ONLY if they visited the first site.

Example: User visits site1, then visits site2, redirect them back to site1. User visits site2, but not site1, keep them on site2, don't redirect them.

There's got to be a simple way to do this? How would you do it?

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum