0

I started by using the method described here in order to create a Mobile redirect, and it works perfectly.

What I need to do next, however, is prevent it from happening on any page other than the homepage. In other words: If the user loads the homepage from a mobile device, the redirect should happen - but if they load any other page from a mobile device, the redirect should not occur.

Example:


homepage URL = redirect


blog URL = NO-redirect

about us URL = NO-redirect

etc etc etc


Here is the code that I have currently:

# 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.com]

# Failed attempt at redirecting only homepage
RewriteCond %{HTTP_HOST}  ^(domain\.com|www\.domain\.com)$ [NC]
# Check if we're just going to the homepage - fail
RewriteCond %{REQUEST_URI} ^/(index.php)?$ 

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 %{HTTP_HOST}          !^m\.

# 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 ^(index\.php)?$ http://m.domain.com [R,L]

And that didn't work. What do I do?

Community
  • 1
  • 1
hdwebpros
  • 528
  • 3
  • 7
  • 20
  • 1
    Your problem is probably related to using [OR]. It's not clear the rules you are using from the linked SO answer. Please include all the rewrite rules. – h0tw1r3 Jun 21 '13 at 17:42
  • Thanks for your quick reply. I edited the post to show my current code. I took out the [OR] but still no go :( – hdwebpros Jun 21 '13 at 18:22
  • @hdwebpros So what you're saying is that the rest of the file is unchanged, you just added the two rows with `index.php` and `/` and it stops redirecting at all? – Joachim Isaksson Jun 21 '13 at 19:52
  • Thanks Joachim. I edited the file to Jon Lin's suggestion. It still doesn't work. By that I mean it redirects every web request to the mobile device, even if I'm initially trying to access an interior page (such as the blog) – hdwebpros Jun 21 '13 at 21:08

3 Answers3

1

The rewrite rules below were tested in an .htaccess file in the document root. Replace m.domain.com and domain.com as appropriate.

Additionally you can force mobile on or off for any URL by appending ?m=0 or ?m=1, or setting the mredir cookie with the scripting language (php in this case).

RewriteEngine On

# Put forced mobile state into 'mredir' cookie
RewriteCond %{QUERY_STRING} (?:^|&)m=([0-1])(?:&|$)
RewriteRule ^ - [CO=mredir:%1:.domain.com]

# Forced mobile? (m=1)
RewriteCond %{HTTP_HOST}    !^m\.domain\.com$
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$) [OR]
RewriteCond %{HTTP_COOKIE}  ^.*mredir=1
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteRule ^               http://m.domain.com [R=302,L]

# Forced regular? (m=0)
RewriteCond %{HTTP_HOST}    ^m\.domain\.com$
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) [OR]
RewriteCond %{HTTP_COOKIE}  ^.*mredir=0
RewriteCond %{QUERY_STRING} !(^|&)m=1(&|$)
RewriteRule ^               http://domain.com [R=302,L]

# Auto redirect to mobile if not forced above
RewriteCond %{HTTP_HOST} !^m\.domain\.com$
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteCond %{HTTP_COOKIE} !.*mredir=0
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteCond %{HTTP:x-wap-profile}%{HTTP:Profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv|palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda|xda-|up.browser|up.link|windowssce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
# Only auto-redirect on / or /index.php
RewriteRule ^(\/|index\.php)?$ http://m.domain.com [R=302,L]
h0tw1r3
  • 6,618
  • 1
  • 28
  • 34
  • Thank you. I'm sure your answer will be much more beneficial than my stumbled upon (but working) solution – hdwebpros Jun 26 '13 at 20:42
  • Also, your solution would not work how you requested if the URL was `/index.php`. That RewriteCond really did nothing useful anyway. – h0tw1r3 Jun 26 '13 at 20:50
0

Your problem is here:

RewriteCond %{REQUEST_URI} ^/index.php$ 
RewriteCond %{REQUEST_URI} ^/$

The request uri cannot be both /index.php AND / at the same time. This regex needs to be combined into an either/or:

RewriteCond %{REQUEST_URI} ^/(index.php)?$ 
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks Jon Lin. I tried your suggestion. It makes sense to me now. It still doesn't redirect though. By that I mean it redirects every web request to the mobile website, even if I'm initially trying to access an interior page (such as the blog) – hdwebpros Jun 21 '13 at 21:46
  • @hdwebpros You could alternatively get rid of that condition entirely, and change the rule to: `RewriteRule ^(index\.php)?$ http://m.domain.com [R,L]` – Jon Lin Jun 21 '13 at 22:33
  • Thanks again. Just tried that. Still no go. :( I type in the blog URL (domain.com/blog) and it still redirects me to the m. site – hdwebpros Jun 22 '13 at 12:18
  • Do you have any other ideas to why it won't do that? – hdwebpros Jun 25 '13 at 16:56
0

For some freak reason, for now, this is working?!?!?

RewriteCond %{REQUEST_URI} (.*)/$

Why, I have no idea

hdwebpros
  • 528
  • 3
  • 7
  • 20