Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question What do I add to this code to redirect based on 2 cookie values?

    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?

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_COOKIE1} cookie1=true
    RewriteCond %{HTTP_COOKIE2} cookie2=true
    RewriteRule .* /page3.html
    Thanks
    Last edited by thewiizard; 05-08-2010 at 01:42 PM.

  • #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    Try this?
    Code:
    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/ht...ml#modrewrite2
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • #3
    New Coder
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

    Code:
    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

  • #4
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    Code:
    # 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".
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • Users who have thanked Samhain13 for this post:

    thewiizard (05-09-2010)

  • #5
    New Coder
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.)

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

  • #6
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    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".
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • #7
    New Coder
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

    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..

    Code:
    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).

    Code:
    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)

    Code:
    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

  • #8
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    Strange, I just tested this and it works fine.

    Code:
    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.
    Last edited by Samhain13; 05-10-2010 at 07:49 PM.
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • #9
    New Coder
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #10
    New Coder
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •