Mobile browser redirection with mod_rewrite

Matthew Nuzum —  — Leave a comment

I found a great article on using mod_rewrite to send mobile browsers to a special page or site. I modified it to do the inverse as well. You can use the rules demonstrated in there to send mobile browsers to your mobile site and you can use the rules below as an inverse to send desktop browsers from your mobile site to your normal site.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_ACCEPT} !"text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC]
RewriteCond %{HTTP_USER_AGENT} !"acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"dang|doco|erics|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC]
RewriteCond %{HTTP_USER_AGENT} !"palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC]
RewriteCond %{HTTP_USER_AGENT} !"sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC]
RewriteCond %{HTTP_USER_AGENT} !"teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC]
RewriteCond %{HTTP_USER_AGENT} !"wapp|wapr|webc|winw|winw|xda|xda-" [NC]
RewriteCond %{HTTP_USER_AGENT} !"up.browser|up.link|windowssce|iemobile|mini|mmp" [NC]
RewriteCond %{HTTP_USER_AGENT} !"symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} macintosh [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/ [L,R=302]

Three things to note about this compared to the original:

  1. My use case is two sites, www.yoursite.com for normal and m.yoursite.com for mobile sites
  2. I only check URLs on the homepage, I figure here that if they’re on an interior page they’re there on purpose
  3. You understand mod_rewrite and regular expressions enough to know what to change above to make it right for you (only the 2nd and last lines will likely need to change)
If it helped, please share!Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

Matthew Nuzum

Posts Twitter Facebook

Web guy, big thinker, loves to talk and write. Front end web, mobile and UX developer for John Deere ISG. My projects: @dsmwebgeeks @tekrs @squaretap ✝