1

I've adapted a htaccess file that was provided here Mobile Redirect using htaccess however, what I'm trying to do is to check the user is not already in the subfolder(rather than sub domain) domain.co.uk/mobile and if not, redirect them to the mobile site.

However, this just drops into an infinite loop and therefore fails.

Can anyone tell me what the problem is and how I do this kind of RewriteCond?

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.domain.co.uk]

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{REQUEST_URI} !^/mobile/ [NC]
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://www.domain.co.uk/mobile [R,L]
Community
  • 1
  • 1
Cliftwalker
  • 369
  • 1
  • 5
  • 17
  • You could try changing your mobile site check from `!^/mobile/` to `!^[/]?mobile` - from what I've read in the past, sometimes the leading `/` isn't in the REQUEST_URI. Also, you could try enabling the rewrite log. – kitti Nov 01 '12 at 21:52
  • Ryan, that appears to of done the trick! Thank you for very much for that. If you want to put that in as an answer, I'll do some more testing then accept? – Cliftwalker Nov 01 '12 at 21:58
  • Done. Didn't put it as an answer before because it was just a guess. :) – kitti Nov 01 '12 at 22:00

1 Answers1

1

You should change your mobile site check from !^/mobile/ to !^[/]?mobile. Apache does not always include the leading slash in the REQUEST_URI. This regex makes that leading slash optional, so the rule will work whether the slash is present or not.

kitti
  • 14,663
  • 31
  • 49