Rosh
Rosh

Reputation: 1736

rewriting url for tablet and smart phone

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)?

Upvotes: 0

Views: 2397

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

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.

Upvotes: 2

Related Questions