Results 1 to 3 of 3

Thread: [RESOLVED] Apache RewriteRule fails to check cookie value

  1. #1

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Resolved [RESOLVED] Apache RewriteRule fails to check cookie value

    I'm setting a cookie value using RewriteRule statements in .htaccess for checking if my site should not redirect from the main desktop site to the mobile site (when accessed via iphone browser). The problem occurs when the first RewriteRule sets "mobile=desktop" in the cookie (from detecting "?desktop=true" in the querystring), the third RewriteRule fails to detect the new cookie value and redirect. If I reload the page again (without any querystring for "?desktop=true"), then the third RewriteRule redirects successfully.

    Can anyone tell me why the third RewriteRule fails to detect the cookie value straight after it is set in the first rule? How can I fix this to work properly?

    Code:
    # set cookie if true querystring
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{QUERY_STRING} (.*)desktop=true(.*)$
    RewriteRule ^(.*) - [CO=mobile:desktop:example.com]
    
    # unset cookie if false querystring
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{QUERY_STRING} (.*)desktop=false(.*)$
    RewriteRule ^(.*) - [CO=mobile:mobile:example.com]
    
    # Redirect iPhone users to mobile website
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{HTTP_COOKIE} !mobile=desktop
    RewriteRule ^(.*) http://m.example.com/$1 [L,R]
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Apache RewriteRule fails to check cookie value

    I could be wrong, but I don't think you can set and read a new cookie on the same pass. The server doesn't obtain cookie data until it is sent along with a client request; so the cookie must be present on the client side before the server can know about it. Your RewriteRule that sets the cookie is sending an HTTP header to the client to set a cookie; any requests after that will send the cookie to the server, as you experienced.

  3. #3

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Apache RewriteRule fails to check cookie value

    Thanks for the info.

    A long-distant memory of boolean logic got me to the following which appears to work:

    Code:
    # set cookie if true querystring
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{QUERY_STRING} (.*)d=true(.*)$
    RewriteRule ^(.*) - [CO=mobile:desktop:example.com]
    
    # unset cookie if false querystring
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{QUERY_STRING} (.*)d=false(.*)$
    RewriteRule ^(.*) - [CO=mobile:mobile:example.com]
    
    # Redirect iPhone users to mobile website
    # if ?d != true AND cookie unset
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{QUERY_STRING} !(.*)d=true(.*)$
    RewriteCond %{HTTP_COOKIE} !mobile=desktop
    RewriteRule ^(.*) http://m.example.com/$1 [L,R]
    
    # Redirect iPhone users to mobile website
    # if ?d != true AND ?d = false
    RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [NC]
    RewriteCond %{QUERY_STRING} !(.*)d=true(.*)$
    RewriteCond %{QUERY_STRING} (.*)d=false(.*)$
    RewriteRule ^(.*) http://m.example.com/$1 [L,R]
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

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



Click Here to Expand Forum to Full Width