homepage Welcome to WebmasterWorld Guest from 54.166.53.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess redirect for smart phone vs. tablets
How to split it along those two sizes
dalyea



 
Msg#: 4411920 posted 6:03 pm on Jan 28, 2012 (gmt 0)

I'd like to have .htaccess code that will redirect users to either a mobile version of my site (320x240 sizing) or a tablet sizing (whatever that turns out to be). In the past year or so, I've been redirecting too many iPad and Galaxy users to the too-small mobile version of my site.

Here is what I have as of now. My site is www. of course and my mobile site is m. Some day, my tablet site will be t.

# Mobile devices - / only
RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_USER_AGENT} !".*iPad.*|.*Galaxy.*" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
RewriteRule ^/$ [m.mysite.com...] [L,R=301]

# Tablet devices - / only
?

I will handle additional pages and cookie-ing so that people can elect to view my full site later. For now, I just want a clean split between mobile and tablet. I know I need more tablet devices, so I figure I'll gather those over time. Unless - there is some catch-all I can use in .htaccess to differentiate mobile vs. tablets.

Does anyone have a tried and true way to do this reliably?

As a part B. to this question, do some people put JS in their m. site version to split out in the code a mobile sized site and a tablet sized site? I'm thinking about that first RewriteCond above and how it's really just m. or not m. right now, but what will I do if I have a t. version of my site.

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4411920 posted 12:22 am on Jan 29, 2012 (gmt 0)

Rule #1: use "example.com" (or .net or .org) in your examples. It has to be this exact name because anything else is auto-converted into a clickable link, and then nobody can see what you typed.
RewriteCond %{HTTP_USER_AGENT} !".*iPad.*|.*Galaxy.*" [NC]

You don't need the .* and you don't need the quotation marks. All you need is
!(iPad|Galaxy)
meaning "does not contain the strings 'iPad' or 'Galaxy'". Since you are not capturing and not anchoring, it does not matter what comes before or after. And you almost never need quotation marks in mod_rewrite.

It is generally better not to say [NC] when listing user-agents, unless you have a horrible server that changes the case of everything it sees. Casing tends to be very exact; in fact that's one way to spot robots with made-up UAs.

What does this sequence mean?
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_USER_AGENT} !".*iPad.*|.*Galaxy.*" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

Line 1: UA might contain "iPad"
Line 2: UA must not contain "iPad"
Line 3: UA must not contain the full word "Macintosh". (Is this meant to exclude some type of spoofer? Only full-size Macs use the full word; mobiles only have room for "Mac".)

Using javascript is iffy because the user might have turned it off. Or the user-agent might not have scripting at all.

Is your mobile site m.example.com or is it www.example.com/m ? The REQUEST_URI does not see subdomains. For that you need either THE_REQUEST or HTTP_HOST.

dalyea



 
Msg#: 4411920 posted 5:54 am on Jan 29, 2012 (gmt 0)

Hi Lucy,

Thanks for the reply. Yes, having ipad in line 1 is not needed, you're right. And I didn't think I needed those trailing .* since I'm not capturing any variable values. I thought double quotes were appropriate to surround the string iPad|Galaxy. Would these be equivalent?

RewriteCond %{HTTP_USER_AGENT} !"iPad|Galaxy" [NC]
RewriteCond %{HTTP_USER_AGENT} !(iPad|Galaxy) [NC]

And per your suggestion, knock off the NC at least on this
one line. I will leave it in place on the first line.

The macintosh thing I saw somewhere as way to make sure it's not another Mac device or computer of some sort. So you think I could safely do away with that, right?

My site is m.mysite.com, though it is at docroot/m. The above code works fine, and has, for all of 2011, minus the ipad|galaxy part. So REQUEST_URI works fine. I have JS on my homepage that handles cookie-ing if someone clicks "full site" on my m. site, and it works, too.

My end goal is - have any smart phone with a screen size of (about?) 320x240 redirect to my m. mobile site, but make sure that no tablets (> 320x240) go there. But I don't want to do this with JS for all the reasons that are discussed in other threads. I'm just looking for the cleanest (current) code to make the split.

Thanks again!

=============================================================


Rule #1: use "example.com" (or .net or .org) in your examples. It has to be this exact name because anything else is auto-converted into a clickable link, and then nobody can see what you typed.
RewriteCond %{HTTP_USER_AGENT} !".*iPad.*|.*Galaxy.*" [NC]

You don't need the .* and you don't need the quotation marks. All you need is
!(iPad|Galaxy)
meaning "does not contain the strings 'iPad' or 'Galaxy'". Since you are not capturing and not anchoring, it does not matter what comes before or after. And you almost never need quotation marks in mod_rewrite.

It is generally better not to say [NC] when listing user-agents, unless you have a horrible server that changes the case of everything it sees. Casing tends to be very exact; in fact that's one way to spot robots with made-up UAs.

What does this sequence mean?
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_USER_AGENT} !".*iPad.*|.*Galaxy.*" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

Line 1: UA might contain "iPad"
Line 2: UA must not contain "iPad"
Line 3: UA must not contain the full word "Macintosh". (Is this meant to exclude some type of spoofer? Only full-size Macs use the full word; mobiles only have room for "Mac".)

Using javascript is iffy because the user might have turned it off. Or the user-agent might not have scripting at all.

Is your mobile site m.example.com or is it www.example.com/m ? The REQUEST_URI does not see subdomains. For that you need either THE_REQUEST or HTTP_HOST.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4411920 posted 8:02 am on Jan 29, 2012 (gmt 0)

No. You do not need the quotes at all. Use parentheses.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4411920 posted 9:49 am on Jan 29, 2012 (gmt 0)

Oh, incidentally: The full name of Googlebot-Mobile is:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)

So if your Condition says "iPhone" you will already be picking up the Googlemobile without having to code for it separately. Throw in the word "Mobile" and you've got yourself covered from both sides.

And it's a pity the iPad and iPod are on different sides of the screen-size split, because otherwise you could simply say iP[ao]d ;)

My, what a lot of BlackBerry UAs there are. I just detoured to thumb through raw logs. But it's always capitalized like that.

Which reminds me: One solid exception to the "case-sensitive" guideline: If you're locking out a particular UA, setting [NC] might also lock out things that are pretending to be that UA. You probably don't want them either. Unless the thing you're locking out is itself a faker, identified by its wrong casing. But legitimate UAs like "iPhone" are always going to be written like that. You could say "iP", case-sensitive, and cover half a dozen devices at once :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved