htaccess 使用 cookie 重定向多个域以记住所有内容
我有一个客户的 2 个网站。一种是移动网站,另一种是普通网站。它们都有单独的 URL。
使用 .htaccess 我已经让它工作了,这样如果您使用移动设备进入,您就会被重定向到移动网站。我现在想做的是在移动网站上有一个链接,可以将用户带回正常网站。此设置将被设置的 cookie 记住。
这就是问题所在...
我可以为移动网站设置 cookie,但是当它重定向到普通网站时,由于跨域,它看不到该 cookie,并直接重定向回移动网站。
我试图变得聪明,使 cookie 放置动态依赖于域,并且当重定向到正常站点时,也在那里设置一个 cookie 告诉它不要尝试重定向。
这是我正在处理的 mod_rewrite 代码。现在对此感到头疼,所以如果有人能指出我哪里出了问题或者这是否可能,那就太好了
# 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=$1 [L,QSA]
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=$1 [L,QSA]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会更改移动网站上的链接,使其不指向普通网站的真实起始页。相反,创建一个名为“nomredir”的页面。当访问该页面时,它应该设置您将要检查的 cookie,然后它应该将用户重定向到起始页面。这样,您就可以在正确的域中设置 cookie,并且仅针对那些专门请求它的用户。
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.