2

I have a mobile site and a desktop site. I have set up automatic redirection for mobile users if they access the desktop site. But unfortunately I need them to access the full site as well, if they prefer.

I have set up a cookie in the .htaccess file, how do i check to see if the cookie has been set so they can access the full site? Haven't found a clear answer.

Code:

#redirect
RewriteCond %{HTTP_HOST} !^m\.stage.sunjournal\.com$
RewriteCond %{HTTP_USER_AGENT} "android|iPhone|blackberry|ipad|iemobile|operamobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ http://m.stage.sunjournal.com/$1 [L,R=302,CO=mobile:yes:m.stage.sunjournal.com:0:/]
BDUB
  • 387
  • 6
  • 18

2 Answers2

0

Mark -- In the reference link you provided, the cookie is set to destroy itself after the user has exited their session. This is because the lifetime of the cookie was never specified. For more information about this, go to the official documentation for mod_write.

BDUB, I would suggest you check out that link that Mark provided. I'm running into issues setting the new cookie, because if a cookie is set for mobile=0, my cookie mobile=1 is not set. I have seen this in my RewriteLogs.

Hectron
  • 203
  • 1
  • 6
  • 14
0

Add this kind of rewrite condition for your rule:

RewriteCond %{HTTP_COOKIE} mobile=yes

This will allow rule to execute if cookie with the name mobile and value of yes is present (to be more precise, that pattern will match any cookie name that ends with mobile (e.g. notmobile) and value that starts with yes (e.g. yesSir)).

If you need this to be more precise (exact match only) -- you have to check how raw cookie look like and adjust pattern accordingly. But as long as you have unique cookie name and value this will not be required.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • so the cookie is set, but what is the logic for trying to access the full site? Is it something with {REQUEST_URI} condition? `RewriteCond %{HTTP_COOKIE} mobile=yes RewriteCond %{REQUEST_URI} !^stage.sunjournal.com [NC] RewriteRule ^(.*)$ http://stage.sunjournal.com/$1 [L,R=302]` – BDUB Sep 22 '11 at 15:15
  • So .. they are on mobile site but want to go to NORMAL site, right? They click special link that sets the cookie and redirects. I think you have to change the logic: **1)** In this case cookie should be like `mobile=no`, **2)** cookie should be set by script and not by the above rule (remove cookie setup from that rule) and **3)** condition to your rule should be `RewriteCond %{HTTP_COOKIE} !mobile=no` (which will also work if no cookie set up yet). Unfortunately I do not have any mobile devices to test this right now and therefore cannot guarantee 100% success straight away. – LazyOne Sep 22 '11 at 15:31
  • Yes there is a link "to view full site" on the mobile. I have set the cookie with php to read mobile=no upon load for the mobile site. My .htaccess file as follows: `RewriteCond %{HTTP_HOST} !^m\.stage.sunjournal\.com$ RewriteCond %{HTTP_COOKIE} !mobile=no RewriteCond %{HTTP_USER_AGENT} "android|iPhone|blackberry|ipad|iemobile|operamobile|palmos|webos|googlebot-mobile" [NC] RewriteRule ^(.*)$ http://m.stage.sunjournal.com/$1 [L,R=302] ` does not do anything new. From a phone i still cannot access the full site, because it directs me back. Do you have a full htaccess entry you can show me? – BDUB Sep 22 '11 at 20:27
  • How much control do you have over your Apache? If you can edit server config files, then enable rewrite debugging ('RewriteLogLevel 9`) and check rewrite log for details (after restarting Apache -- but do not do it on busy server as rewrite log may become huge in few minutes). ALSO - make sure that you setting the cookie to the right domain -- it should be `stage.sunjournal.com` and not `m.stage.sunjournal.com` – LazyOne Sep 22 '11 at 21:30