Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created August 30, 2011 06:14
Show Gist options
  • Select an option

  • Save RichardBronosky/1180308 to your computer and use it in GitHub Desktop.

Select an option

Save RichardBronosky/1180308 to your computer and use it in GitHub Desktop.
Epic Mobile mod_rewrite RewriteRules!
# Define the MOBILE_PATTERN (for user agent detection) ONLY for pages who are NOT black listed from mobile. (TODO: Try using a whitelist instead. Would probably be shorter.)
RewriteCond %{REQUEST_URI} !^/(ads|alerts|answers|audioclips|bands|categories|categories|events|documents|drinkspecials|events|features|forums|gallery|instruments|maps|mp3|lsp|onthestreet|photo|photos|galleries|photos|places|playlists|podcasts|polls|pr|qa|questionnaires|recipes|restaurants|restaurants|specials|search|songs|special_coverage|staff|stories|traffic|tumble|videos) [NC]
RewriteRule .* - [E=MOBILE_PATTERN:iphone|ipod|android|blackberry|symbian|nokia|sonyericsson|sie-|opera\ mini|portalmmm/|windows\ ce|up.browser|up.link|802sh|alcatel-|amoi-|attws|au-mic|audiovox|avantgo|benq|cdm-|ericsson|ericy-|gs308|hitachi-p300|htc-|i-2100|inn|kwc-|lg-|lg/u|lge-|lge/u|lowestcd|mo0|mobilephone|mot-|motorola|n5-10|nec-|opwv|owg1|palm|panasonic|pg-c|philips|pls|pm-|pn-215|profile/midp|qci-|rl-|rpt-httpclient|sagem|samsung|sanyo|sch-a|sch-n|scp-|sec-|sendox|sgh|sharp|smartphone|spha|v60t|vi600|vk530|vm4050|vodafone|wapper|wildseed|winwap-pro]
# NOTICE: The RewriteCond pattern below must be kept exactly in sync with the definition of MOBILE_PATTERN in the RewriteRule above. Make all changes in both places.
# Detect mobile agents and bare domains. Set a redirect flag if found. Flag will be used a few block below
RewriteCond %{HTTP_USER_AGENT} iphone|ipod|android|blackberry|symbian|nokia|sonyericsson|sie-|opera\ mini|portalmmm/|windows\ ce|up.browser|up.link|802sh|alcatel-|amoi-|attws|au-mic|audiovox|avantgo|benq|cdm-|ericsson|ericy-|gs308|hitachi-p300|htc-|i-2100|inn|kwc-|lg-|lg/u|lge-|lge/u|lowestcd|mo0|mobilephone|mot-|motorola|n5-10|nec-|opwv|owg1|palm|panasonic|pg-c|philips|pls|pm-|pn-215|profile/midp|qci-|rl-|rpt-httpclient|sagem|samsung|sanyo|sch-a|sch-n|scp-|sec-|sendox|sgh|sharp|smartphone|spha|v60t|vi600|vk530|vm4050|vodafone|wapper|wildseed|winwap-pro [NC]
RewriteCond %{HTTP_HOST} "!^(m|www)\..+" [NC]
RewriteRule .* - [E=REDIRECT_MOBILE:1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment