{"id":718,"date":"2012-08-22T13:34:59","date_gmt":"2012-08-22T13:34:59","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=718"},"modified":"2012-08-22T13:56:18","modified_gmt":"2012-08-22T13:56:18","slug":"mobile-redirection-apache","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/mobile-redirection-apache\/","title":{"rendered":"Mobile redirection using only Apache"},"content":{"rendered":"<p>With a few mobile sites under my belt over the past couple years, the mobile development process has become easier. Naturally, with each new site, I try to improve the process just a little bit over the last development project.<\/p>\n<p>This time around, I started to ponder the method used to detect mobile devices and redirect them to the mobile site.<\/p>\n<p>For many, this method involves USER_AGENT detection in their scripting language, then a simple redirect to the mobile site.<\/p>\n<p>For others, a web service is used to detect the user agent.<\/p>\n<p>I&#39;m the kind of guy that likes to squeeze every bit of performance juice possible out of any given task. So, in this case, sending a request to a page, then having that page redirect to another page just didn&#39;t seem optimal.<\/p>\n<p>Where do requests go first? The web server, of course. So why can&#39;t we take care of all mobile redirection at the web server level. I&#39;m sure many people are thinking, at this point, &quot;duh, just use a mod_rewrite condition to check the USER_AGENT string&quot;. That works, no doubt.<\/p>\n<p>But, what about storing the user&#39;s preference for desktop or mobile site in a cookie, and directing the user to the appropriate site based on that preference? It has to be done in the scripting language, right? I used to think the same thing about 3 hours ago!<\/p>\n<p>Check out these two virtual host stanza&#39;s:<\/p>\n<p><code>&lt;VirtualHost *:80&gt;<br \/>\n\t&nbsp; ServerName m.yoursite.com<br \/>\n\t&nbsp; DocumentRoot \/var\/www\/html\/yoursite_com\/mobile<br \/>\n\t&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteEngine On<br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteCond %{QUERY_STRING}&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mobile=true<br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteRule ^(.*) - [co=mobile:INVALID:.yoursite.com:-1]&nbsp; <br \/>\n\t&nbsp;&lt;\/VirtualHost&gt;<\/code><\/p>\n<p><code>&lt;VirtualHost *:80&gt;<br \/>\n\t&nbsp; ServerName www.yoursite.com<br \/>\n\t&nbsp; DocumentRoot \/var\/www\/html\/yoursite_com<\/p>\n<p>\t&nbsp; RewriteEngine On<\/p>\n<p>\t&nbsp;&nbsp;&nbsp; RewriteCond %{HTTP_USER_AGENT} &quot;android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos&quot; [NC]<br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteCond %{QUERY_STRING}&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; !mobile=false<br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteCond %{HTTP_COOKIE}&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; !^.*mobile.*$ [NC]<br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteRule ^\/(.*) http:\/\/m.yoursite.ccom\/ [L,R=302]<\/p>\n<p>\t&nbsp;&nbsp;&nbsp; RewriteCond %{QUERY_STRING}&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mobile=false<br \/>\n\t&nbsp;&nbsp;&nbsp; RewriteRule ^(.*) - [co=mobile:false:.yoursite.com:2592000:\/]<\/p>\n<p>\t&nbsp; &lt;\/VirtualHost&gt;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>There you have it. The entire load of detection and storing the users preference can be done entirely in Apache, by leveraging mod_rewrites ability to set and retrieve cookies.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With a few mobile sites under my belt over the past couple years, the mobile development process has become easier. Naturally, with each new site,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[132,41],"tags":[197],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/718"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=718"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":721,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/718\/revisions\/721"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}