Forum Moderators: phranque

Message Too Old, No Replies

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

         

fretfull

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

5+ Year Member Top Contributors Of The Month



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.

phranque

2:11 am on Feb 2, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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 (...)

lucy24

2:40 am on Feb 2, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

phranque

5:09 am on Feb 2, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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]:

tangor

5:55 am on Feb 2, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



An alternative to a rewrite is coding the site responsive and using viewport to determine how content is displayed.

lucy24

6:43 am on Feb 2, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

phranque

9:51 am on Feb 2, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.