Skip to content

Instantly share code, notes, and snippets.

@benclark
Created January 29, 2013 19:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benclark/4667006 to your computer and use it in GitHub Desktop.
Save benclark/4667006 to your computer and use it in GitHub Desktop.
Mobile detection in .htaccess file
###
# BEGIN MOBILE REDIRECTS
###
# If the skipMobileDetection cookie is set, and we're on the mobile domain,
# return to the normal domain. Do this only for SSL as Varnish will handle
# for port 80.
RewriteCond %{HTTP_COOKIE} skipmobiledetection [NC]
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^.*$ https://www.example.com/$0 [R=302,L]
# If mobile and NOT on the mobile domain, check whether we should redirect
# based on the URL (path). Do this only for SSL as Varnish will handle the
# redirects for port 80.
RewriteCond %{HTTP_USER_AGENT} android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|meego.+mobile|midp|mmp|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC]
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteCond %{SERVER_PORT} =443
# Make sure skipMobileDetection is NOT set.
RewriteCond %{HTTP_COOKIE} !skipmobiledetection [NC]
RewriteRule ^((secure|donate|blog/.+|mobile|wakeup/pledge)/?.*)?$ https://m.example.com/$0 [R=302,L]
# If NOT mobile and on the mobile domain, return to the normal domain. Do
# this only for SSL as Varnish will handle the redirects for port 80.
RewriteCond %{HTTP_USER_AGENT} !android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|meego.+mobile|midp|mmp|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC]
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^.*$ https://www.example.com/$0 [R=302,L]
###
# END MOBILE REDIRECTS
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment