0

I'm using Apache's rewrite url. .htaccess file contains following redirect rules. My requirement is that, when ever I hit on the url www.temp.com/card, based on the device it should internally call that particular .do.

For Example, from a smart phone if i type www.temp.com/card internally it should call www.temp.com/pa/spCard.do.

I tried with the below rules but problem is that some tablets having android, so in tablet it always going into smarphone .do.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^card/?$ /pa/spCard.do?uri=$1 [QSA]

RewriteCond %{HTTP_USER_AGENT} "ipad" [NC]
RewriteRule ^card/?$ /pa/tabCard.do?uri=$1 [QSA]

RewriteRule ^card/?$ /pa/webCard.do?uri=$1 [QSA]
</IfModule>

Is there any way we can distinguish between an Android mobile and tablet (using user agent string)?

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
Rosh
  • 1,676
  • 4
  • 21
  • 35

1 Answers1

2

You must stop further rewriting by adding the L flag, when you have detected a device

RewriteRule ^card/?$ /pa/spCard.do?uri=$1 [L,QSA]

And you don't have $1 captured anywhere in the rewrite rules. Furthermore, the URL is always card, so you can reduce the rule to

RewriteRule ^card/?$ /pa/spCard.do [L]

or

RewriteRule ^card/?$ /pa/spCard.do?uri=$0 [L,QSA]

if you need the uri parameter.

Same with tabCard.do and webCard.do, of course.

To distingiush between Android phones and tablets, see Chrome for Android User-Agent. This does not hold for all tablets, unfortunately. But at least, you can weed out some of the tablets by specifying android.*mobile with the first rule and just android with the second

RewriteCond %{HTTP_USER_AGENT} android.*mobile|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile [NC]
RewriteRule ^card/?$ /pa/spCard.do?uri=$0 [L,QSA]

RewriteCond %{HTTP_USER_AGENT} android|ipad [NC]
RewriteRule ^card/?$ /pa/tabCard.do?uri=$0 [L,QSA]

With these conditions, you will know that you have a tablet in the second rule and a phone or tablet in the first rule.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • :Is there any way we can distinguish between an mobile and tablet (using user agent string)? tried a few rules, but its difficult to distinguish between an Android mobile and tablet . – Rosh Apr 22 '13 at 09:01
  • 1
    @Rosh The difference seems to be `Mobile`, see https://developers.google.com/chrome/mobile/docs/user-agent for details. Although, there seem to be tablets having `Mobile` in their user agent string, see the comments to this answer for example http://stackoverflow.com/a/5344382/1741542 – Olaf Dietsche Apr 22 '13 at 09:24
  • checked with android tablet and phone. Tablet user agent is having mobile in it. – Rosh Apr 22 '13 at 09:39