htaccess redirects over multiple domains using cookies to remember it all
I've got 2 sites for a client. One is the mobile site, the other is the normal site. These both have seperate URLs.
Using .htaccess I've already got it working so that if you come in on a mobile, you get redirected to the mobile site. What I would like to do now is have a link on the mobile site that will take a user back to the normal site. This setting would be remembered by a cookie being set.
And thats where the problem comes in...
I can set the cookie for the mobile site but when its then redirected to the normal site, it cant see that cookie due to cross-domains and redirects right back to the mobile site.
Im trying to be clever and making the cookie placement dynamic dependant on domain and when redirecting to the normal site, setting a cookie there too to tell it not to try and redirect.
Here's the mod_rewrite code that Im working on. Have brain ache over this right now so if someone could point out where I've gone wrong or if this is even possible, that would be grand
# Write cookie if redirected
RewriteCond %{QUERY_STRING} (^|&)mredir=0(&|$)
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST}]
# redirect if mobile device && !mredir
RewriteCond %{HTTP_HOST} ^normalURL.com$
RewriteCond %{HTTP_COOKIE} !mredir=0
RewriteCond %{QUERY_STRING} !(^|&)mredir=0(&|$)
RewriteCond %{REQUEST_URI} !^/mobile/
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ http://mobileURL.com/ [R=302,QSA,L]
# point at mobile site IF !mredir
RewriteCond %{HTTP_HOST} ^mobileURL.com$
RewriteCond %{HTTP_COOKIE} !mredir=0
RewriteCond %{QUERY_STRING} !(^|&)mredir=0(&|$)
RewriteCond %{REQUEST_URI} !^/mobile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^(.*)$ mobile/$1 [L,QSA]
# IF mredir, redirect normal site
RewriteCond %{HTTP_HOST} ^mobileURL.com$
RewriteCond %{HTTP_COOKIE} mredir=0 [OR]
RewriteCond %{QUERY_STRING} (^|&)mredir=0(&|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^(.*)$ http://normalURL.com?mredir=0 [L,QSA]
# point at normal site
RewriteCond %{HTTP_HOST} ^normalURL.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q开发者_JAVA百科=$1 [L,QSA]
I would change the link on the mobile site to not point at the real start page of the normal site. Instead, create a page called e.g. "nomredir". When that page is accessed, it should set the cookie that you will be checking, and then it should redirect the user shouldto the start page. That way you get the cookie set in the proper domain and only for those users who specifically request it.
精彩评论