Welcome to WebmasterWorld Guest from 54.242.115.55

Forum Moderators: Ocean10000 & phranque

Using rewriterule to direct when to send traffic to mobile site?

     
11:44 pm on Feb 1, 2019 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:May 7, 2018
posts:81
votes: 6


I'm currently using a PHP library to look at the user agent and determine whether a desktop or mobile site is served up. I'm hoping to do something more efficient and use a small series of rewriterules instead. Has anyone gone this route? I'm concerned that it might miss some mobile devices. The PHP library does a brute force compare to a database of user agent strings. A series of rewriterules would be more superficial looking for keywords like "Android" in the user agent string.
2:11 am on Feb 2, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11613
votes: 195


i would start with an analysis of all user agent strings that appear in a representative sample of your server access logs.
then find the regular expression that filters out the set of user agents you are targeting.

something like this should do the job:
cut (...)access.log|sort -u|egrep (...)
2:40 am on Feb 2, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15507
votes: 752


Here's a simple real-life example:
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod)
RewriteCond %{HTTP_COOKIE} !phonegame
RewriteCond %{HTTP_REFERER} !phone\.html
RewriteRule ^games/(tower|palace|canal|color)/?$ https://www.example.com/games/phone.html [R=302,CO=phonegame:1:.example.com:525600,L]
Translation: If someone requests one of four large games, and these three conditions are all met
-- UA is Android or iPhone/iPod (not iPad)
-- a specific cookie is not present
-- they didn't come directly from "phone.html"
then redirect to "phone.html", and set a cookie saying that this has happened.

The purpose of the cookie is essentially to say “Yes, yes, I know, don’t bug me about this again for the next year” (in the same way that a smartphone user might deliberately choose to view the desktop version of your site), and the referer-check is a backup if the person doesn’t do cookies.

This, incidentally, means the mobile Googlebot and bingbot--and possibly other search engines with iPhone or Android in the UA string, though in practice I only see the Googlebot--get redirected right along with everyone else. Think about whether you want to make exceptions. In my specific case it doesn't matter, because the pages in question are all noindexed; this will be an individual judgement call.
5:09 am on Feb 2, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11613
votes: 195


Googlebot--get redirected right along with everyone else.

as always, make sure you aren't doing anything that (to google) looks like user agent cloaking [support.google.com]:
5:55 am on Feb 2, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:9252
votes: 785


An alternative to a rewrite is coding the site responsive and using viewport to determine how content is displayed.
6:43 am on Feb 2, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15507
votes: 752


as always, make sure you aren't doing anything that (to google) looks like user agent cloaking
The paradox is that if I make an exception and don’t redirect them, then I really am cloaking, because I’d be treating the googlebot differently from a human :) The ordinary non-mobile Googlebot gets the requested page, just like a non-mobile human.

Out of curiosity, I just checked logs because it gives insight into their handling of redirects. One day in December they requested all pages in this directory, with both user-agents, leading to a fistful of 302s and 200s. The first 302 was followed-up immediately (same second in logs, just like a human). The others, at intervals of a minute or so, weren't. This matches my earlier impression that the Googlebot follows redirects unless it happens to have already visited the target page within the last hour or so.
9:51 am on Feb 2, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11613
votes: 195


make sure you aren't doing anything that (to google) looks like user agent cloaking

The ordinary non-mobile Googlebot gets the requested page, just like a non-mobile human.

exactly.