0

I am writing some code that redirects to a mobile verision of a website UNLESS a cookie has been set.

There is a link on the mobile site, "Go to desktop site". It's target is the desktop site with the GET variable, "noredirect=1".

The following is the code in the root .htaccess file on the desktop site. It checks for the GET variable and then sets a cookie if it exists, then skips the next rule.

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=1(&|$)

# Set a cookie to say we want to stay on the desktop site 
# and skip the next rule so 
# that the below mobile rule does not redirect 
# (cookie cannot be set AND read in one request)
RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=1]

The skip flag does not seem to be working. Below this code I have a few RewriteConds and one RewriteRule:

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}     !\smredir=1(;|$)
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera|mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile} !^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?!www\.)[^.]+)\.([^.]+\.[^.]+)$ [NC]
RewriteRule ^$ http://m.%1.%2 [L,R]

Is the skip flag meant to still work when the next rule has conditions preceding it?

Also, my main question: Is the syntax for the skip flag correct and can it be done in the same line as where one is setting a cookie?

I've tried these 2 combinations:

RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=1]

and

RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST}] [S=1]

Neither give any errors but the skip flag still does not work.

Help would be appreciated, thanks.

Daniel
  • 165
  • 1
  • 11
  • Have you inspected the browser cookies to make sure mredir is really set? – ffflabs Jun 24 '14 at 12:39
  • Yes. The cookie does get set. But since the skip function does not seem to work, it immediately redirects back to the mobile site (as it cannot read the cookie in the same request). When I click the link to got to the desktop site from the mobile site again, it works and stays there. I've edited the question to show the second block of code that does the redirect. – Daniel Jun 24 '14 at 13:04
  • What if you set another RewriteCond to perform the redirection only if query string hasn't the ```noredirect``` parameter? – ffflabs Jun 24 '14 at 13:08
  • I've tried adding `RewriteCond %{QUERY_STRING} !(^|&)noredirect=1(&|$)` to the second code block but it's now staying on mobile. maybe the skip tag wasn;t the problem in the first place. I need to do some more debugging... – Daniel Jun 24 '14 at 13:28
  • 1
    You could also add the [L] parameter to the cookie setting rule so your htaccess would stop processing rules. But of course this might break further rules you have in there. Perhaps if you rearrange them? – ffflabs Jun 24 '14 at 13:51
  • I've tried it like this `RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST}] [L]`. But it's like my original question asks. Is that valid? Separate square brackets for the L flag, even though CO is also a flag? – Daniel Jun 24 '14 at 15:30
  • it should be comma separated [CO=mredir:1:%{HTTP_HOST},L] – ffflabs Jun 24 '14 at 17:46

1 Answers1

1

See my Tips for debugging .htaccess rewrite rules for general tips on how to chop this problem. You also need to differentiate response cookies (output) from request cookies (input). The CO flag sets a response cookie, but this isn't necessarily visible in %{HTTP:Cookie} on the current pass. Either use the skip chain correctly or also set an environment variable as well and use it because this is immediately visible.

Community
  • 1
  • 1
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • I've now tried `RewriteRule .? - [CO=mredir:1:%{HTTP_HOST},L]`, `RewriteRule .? - [CO=mredir:1:%{HTTP_HOST},S=1]` and `RewriteRule .? - [CO=mredir:1:%{HTTP_HOST},S=5]`. They all don't skip the remaining rule. Why is that? The cookie always gets set but then it always redirects too. – Daniel Jun 25 '14 at 09:52
  • It's like both the skip flag and the last flag just don't work here? – Daniel Jun 25 '14 at 09:54
  • Found the problem - there was PHP code on the site that another developer had added to do mobile detection and redirection so it wasn't the htaccess I had been working on which cause the problem. Sorry guys! I'll mark this as the answer as it's useful anyway and could have been my problem if that other code didn't exist :) – Daniel Jun 25 '14 at 14:28