Skip to content

Instantly share code, notes, and snippets.

@fenrir-naru
Forked from phillipadsmith/mod_rewrite_rules
Last active September 2, 2015 13:04
Show Gist options
  • Save fenrir-naru/b3a2c97e15aa7f0b88f6 to your computer and use it in GitHub Desktop.
Save fenrir-naru/b3a2c97e15aa7f0b88f6 to your computer and use it in GitHub Desktop.
Mobile site redirection using mod_rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# If the visit is neither html nor directory, stop
RewriteCond %{LA-U:REQUEST_URI} !\.html?$ [NC]
RewriteRule ^ - [L]
# If the user is asking for manual control, set a cookie, remove the query, and stop.
RewriteCond %{QUERY_STRING} ^(.*&)?rewrite=([^&]*)&?(.*)$
RewriteRule .* $0?%1%3 [L,R=301,CO=rewrite:%2:.naruoka.org]
# If there's cookie, no redirection ocures.
RewriteCond %{HTTP_COOKIE} (?:^|;)\ *rewrite=[^;]* [NC]
RewriteRule ^ - [L]
# if the browser accepts these mime-types, it's definitely mobile, or pretending to be
#RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
# a bunch of user agent tests
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Note: I've updated the conditions above to skip iPads. However,
# mobile safari still reports itself as 'mobile' and thus needs an explicit opt-out
RewriteCond %{HTTP_USER_AGENT} !iPad [NC]
RewriteRule ^ - [E=USER_DEVICE:mobile]
RewriteCond %{ENV:USER_DEVICE} mobile
RewriteCond %{LA-U:REQUEST_URI} !\.m\.html?$ [NC]
RewriteCond %{LA-U:REQUEST_FILENAME} (.*)\.(html?)$ [NC]
RewriteRule ^ - [E=REDIRECT_TRY:%1.m.%2]
RewriteCond %{ENV:REDIRECT_TRY} -f
RewriteRule ^ %{ENV:REDIRECT_TRY} [R=302]
RewriteRule ^ - [L,CO=rewrite:auto:.naruoka.org]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment