|
-
Aug 9th, 2011, 02:02 AM
#1
[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]
-
Aug 9th, 2011, 12:14 PM
#2
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.
-
Aug 9th, 2011, 08:44 PM
#3
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]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|