0

If a user is viewing my Website www.myUrl.com with a mobile Browser, I would like to redirect them to mobile.myUrl.com. I read this thread Auto detect mobile browser (via user-agent?), which says, that you should detect the user-agent and then redirect the user.

But I havent found a thread, which tells me HOW to redirect to mobile.myUrl.com. Should and can I use Apache for that? Or should I instead do it inside my Webapp?

Community
  • 1
  • 1
Pascal Klein
  • 23,665
  • 24
  • 82
  • 119
  • possible duplicate of [Auto detect mobile browser (via user-agent?)](http://stackoverflow.com/questions/1005153/auto-detect-mobile-browser-via-user-agent), use apache rewrite **Or** equivalent of rewrite in IIS – ajreal Dec 19 '10 at 13:57
  • @ajreal: The OP has read that, as posted. – Evan Mulawski Dec 19 '10 at 14:01
  • @Evan Mulawski - That answer has included apache rewrite, I don't see any reason to repeat the same question again – ajreal Dec 19 '10 at 14:05
  • @ajreal: The OP wanted an opinion on whether he should use Apache or "do it inside my webapp." It is similar, but certainly not a duplicate. – Evan Mulawski Dec 19 '10 at 14:16
  • @Evan Mulawski - does not 14 upvotes explain everything? – ajreal Dec 19 '10 at 14:19
  • @ajreal: Yes, it does. However, the OP had a thought conflict: "Should and can I use Apache for that? Or should I instead do it inside my Webapp?" This poses another issue. – Evan Mulawski Dec 19 '10 at 14:22
  • Evan Mulawski gave me exactly the answer I wanted: 1.) He showed me how to redirect inside my Webapp with Javascript and 2.) He showed me how to redirect to my subdomain mobile.myUrl.com with Apache. I havent found these answers anywhere else yet. So why am I being downvoted??? – Pascal Klein Dec 19 '10 at 14:23

1 Answers1

3

JAVASCRIPT

First, let me point out that most mobile devices have the ability to disable Javascript.

Now, the most common form of redirection for mobile devices is through Javascript. (Apple's website uses this method.) When the page begins to load, a script is run to determine the User Agent, a browser-specific (and os-specific) string that details the browser type, device type, and/or operating system. This string is then matched against known mobile devices. For example:

<script language=javascript>
<!--
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
   location.replace("http://mobile.myurl.com");
}
-->
</script>

This script would redirect any browser with a user agent containing "iPhone" or "iPod" to "mobile.myurl.com".

This would need to be checked for a bunch of other devices.

Check this site for more information:

http://www.hand-interactive.com/resources/detect-mobile-javascript.htm

If you are using a PHP-compatible server, see this site:

http://www.hand-interactive.com/resources/detect-mobile-php.htm

APACHE

Simply using the mod_rewrite engine, you can redirect browsers with specific user agent strings, just like the Javascript method above:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{HTTP_USER_AGENT} iPod
RewriteRule .* http://mobile.myurl.com/ [R]

This checks for "iPhone" and "iPod" users and redirects them to a mobile version of the site.

More here:

http://www.themepremium.com/htaccess-code-http_user_agent-of-multiple-phone-browsers-for-wordpress-blogs/

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144