1

Possible Duplicate:
Mobile Redirect using htaccess

Currently I have a regular website, utilizing LAMP. I'd like to add a redirect for mobile devices to a subdomain, something like: m.mydomain.com

There's going to be some content differences between the content for PC and mobile devices.

What is the best way to do that? Can it be done with .htaccess file or do I need some sort of PHP script?

Thanks.

Community
  • 1
  • 1
santa
  • 13,148
  • 42
  • 144
  • 230

3 Answers3

6

I think .htaccess would be the fastest. you might put something like this in your htaccess file.

RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ http://m.mydomain.com/$1 [L,R=302]
Bjørne Malmanger
  • 1,437
  • 10
  • 11
  • Yes, I just tried this solution -- works great, however, I need to modify it it to redirect browser back to www.mydomain.com is it's not one of those devices on the list. How do I do that? – santa Feb 23 '12 at 22:02
  • Try starting the condition with an *!*. `RewriteCond %{HTTP_USER_AGENT} !"android|blackberry|...` – Bjørne Malmanger Feb 23 '12 at 22:10
  • I guess I'll put another .htaccess file into my w.mydomain.com subdirectory with RewriteCond %{HTTP_USER_AGENT} !"android|blackberry|... – santa Feb 23 '12 at 22:20
  • 1
    One followup comment to you @santa Sometimes you would like the user to be able to override the settings. i.e. an iPhone user would like to see the www.mydomain.page. This can be achieved by setting an cookie (i.e. "nomobile=true") and do a condition check on that cookie. You could add this condition: `RewriteCond %{HTTP_COOKIE} !^.*nomobile=true.*$ [NC]` – Bjørne Malmanger Feb 24 '12 at 10:21
  • Unfortunately my current www has some drag'n'drop functionality that's not supported by iOs, so i'm OK with iPhone only getting a mobile version at this point. – santa Feb 24 '12 at 15:51
0

Those answers will work fine until you want to separate tablets from mobiles. To get a better match you'll need something like Handset Detection (disclaimer : my startup) which can separate out devices by class (tablet/console/mobile). You can detect serverside with an API kit or on device with Javascript.

Richard
  • 279
  • 3
  • 4
-1

Hi Santa I found this:

 if( navigator.userAgent.match(/Android/i) ||
 navigator.userAgent.match(/webOS/i) ||
 navigator.userAgent.match(/iPhone/i) ||
 navigator.userAgent.match(/iPod/i) ||
 navigator.userAgent.match(/BlackBerry/)
 ){
 // some code
}

Source: What is the best way to detect a mobile device in jQuery?

Community
  • 1
  • 1
Eduardo Iglesias
  • 1,056
  • 16
  • 42
  • 2
    This is a Javascript solution rather than either .htaccess or PHP question the OP asked. The problem with providing a JS solution is that the page has already loaded with content for this to determine OS type. Whereas both .htaccess and PHP will be able to load up alternative content, even redirecting to a subdomain, which is ideal as it's significantly faster and lighter for the user and lighter on the server rather than serving multiple requests. `.htaccess` is the best solution as it performs the redirect at the apache level. – Highway of Life Feb 23 '12 at 21:37
  • Hi oh I see then you need ti execute this before the page paint try this. function preloadFunc() { alert("PreLoad"); } window.onpaint = preloadFunc(); – Eduardo Iglesias Feb 23 '12 at 21:40
  • No, that's still bad to do... you're using Javascript which means it's already happening much too late. Apache > PHP > Javascript. Always work from the Apache level for content delivery or redirection first. If that's not possible, use PHP as you can still throw the location header to an alternate location, but Javascript should only be used as an absolute last resort for redirection. -- And it's not needed in this case. – Highway of Life Feb 23 '12 at 21:48
  • Good point I never think it like that... I will think in a solution with PHP or htaccess – Eduardo Iglesias Feb 23 '12 at 22:02