Skip to content

Instantly share code, notes, and snippets.

@fearphage
Forked from b1tr0t/gist:159014
Created November 8, 2009 01:34
Show Gist options
  • Save fearphage/229016 to your computer and use it in GitHub Desktop.
Save fearphage/229016 to your computer and use it in GitHub Desktop.
###############################################################################
## Mobify Apache Redirect Plugin v1.1 ##
## http://mobify.me ##
## ##
## Installation: ##
## ##
## First: for this to work, you need to configure a mobile site for ##
## yourself using Mobify. Then follow the CNAME setup instructions for ##
## pointing m.yoursite.com to your Mobify account: ##
## http://support.mobify.me/faqs/general/dns ##
## ##
## This code goes in the .htaccess file in the root directory of your ##
## website. If you already have a .htaccess file, generally these lines ##
## should go at the bottom of that file. If you have sophisticated ##
## htaccess redirect logic already, it's possible some of those rules may ##
## prevent these rules for running and you may need to modify your rules or ##
## this plugin. ##
## ##
## For example, if your website is located in /www/yoursite.com/ the ##
## contents of this file should go in /www/yoursite.com/.htaccess ##
## ##
## Do a search and replace on '.yoursite.com' with, well, the domain of ##
## your site. Make sure to include the leading '.'. So you might want to ##
## replace '.yoursite.com' with '.widgetsandneatstuff.net' ##
## ##
## Troubleshooting tips: ##
## ##
## 1) If you have other RewriteRule lines above this plugin in your ##
## .htaccess file, make sure they do not specify the [L] option, as this ##
## option bypasses further RewriteRule tags. ##
## ##
## 2) Make sure you have mod_rewrite installed and enabled in your apache ##
## configuration. Ask your system administrator to verify that this is ##
## the case. ##
## ##
## 3) Your sysadmin can enable a rewrite debugging log file if you're ##
## having trouble. These lines must be placed in the apache global ##
## configuration files for your site and cannot be placed in this ##
## .htaccess file. The lines are as follows: ##
## ##
## RewriteLog "/path/to/log/file/rewrite.log" ##
## RewriteLogLevel 3 ##
## ##
## A word of caution - the lines above should be used for debugging only ##
## as they produce verbose logs that might affect performance on a ##
## busy site. ##
## ##
## 4) Apache mod_rewrite documentation is available here: ##
## ##
## http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html ##
## ##
## ##
###############################################################################
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# CNAME: http://m.yoursite.com/
# COOKIE DOMAIN: .yoursite.com
## First we check for query string parameters. These can be used to force a
## desktop or a mobile view and they override all other rules. To force a
## desktop view, set the parameter ?mobify=0 at the end of your url. To
## force a mobile view, set ?mobify=1 at the end of your url
# check for query string param mobify=0 or mobify=1, set appropriate cookie
RewriteCond %{QUERY_STRING} .*mobify=0.* [NC]
RewriteRule ^(.*)$ - [L,CO=mobify:0:.yoursite.com:120:/]
RewriteCond %{QUERY_STRING} .*mobify=1.* [NC]
RewriteRule ^(.*)$ http://m.yoursite.com/$1 [R,L,CO=mobify:1:.yoursite.com:120:/]
# check for cookie mobify=0 or 1
# if mobify=1 then redirect to the mobile view, and set a cookie
# telling clients to keep using the mobile view for 120 minutes.
RewriteCond %{HTTP_COOKIE} .*mobify=0.* [NC]
RewriteRule ^(.*)$ - [L,CO=mobify:0:.yoursite.com:120:/]
RewriteCond %{HTTP_COOKIE} .*mobify=1.* [NC]
RewriteRule ^(.*)$ http://m.yoursite.com/$1 [R,L,CO=mobify:1:.yoursite.com:120:/]
# 'Positive' mobile matches, check for phone UAs and HTTP headers only set by phones
RewriteCond %{HTTP:HTTP_X_OPERAMINI_PHONE} !="" [NC,OR]
RewriteCond %{HTTP:HTTP_X_MOBILE_GATEWAY} !="" [NC,OR]
RewriteCond %{HTTP:HTTP_ACCEPT} ^.*application/vnd\.wap\.xhtml\+xml.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(alcatel|audiovox|au\-mic|avantgo|bolt|blackberry|blazer|cldc\-|danger|dopod|epoc|ericsson|Google\s+Wireless\s+Transcoder|htc|huawei|iemobile|ipaq|iphone|ipod|j2me|lg|midp|mobile|mot|moto|motorola|nec\-|netfront|netfront|nitro|nokia|novarra\-vision|opera\s+mini|palm|palmsource|panasonic|philips|pocketpc|portalmmm|rover|sagem|samsung|sanyo|sec|series60|sharp|sie\-|smartphone|sony|symbian|t\-mobile|untrusted|up\.browser|up\.link|vodafone\/|wap1\.|wap2\.|webOS|windows\s+ce).* [NC]
RewriteRule ^(.*)$ http://m.yoursite.com/$1 [R,L,CO=mobify:1:.yoursite.com:120:/]
# this runs only if none of the rules above matched. (because of the [L] option used for those rules)
# This means the user is not using a mobile device. We mark this using a cookie
# to speed up future interactions.
RewriteRule ^(.*)$ - [CO=mobify:0:.yoursite.com:120:/]
</IfModule>
###############################################################################
## End Mobify Apache Redirect Plugin v1.1 ##
## http://mobify.me ##
###############################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment