Categories
Tips & How To's

How To Detect Mobile Visitors Using .htaccess Rewrite Rules, Simplified

Since my original posts on mobile redirection in .htaccess files I’ve gotten some comments and emails asking for step-by-step guidance on exactly how to “install” these rules. I hope this post will answer some of those questions.

Before you continue reading, if you have php installed on your server, you may want to consider Andy Moore’s php based detection solution.

The Basics

First off, some basic requirements. You’ll need:

  • Apache/Linux Hosting. Microsoft’s IIS has a completely different method for handling this sort of thing.
  • FTP access.
  • Apache mod_rewrite enabled. Depending on the type of hosting you have, it may be difficult to determine if you have this module installed and it may be impossible to enable if you don’t. If the steps below simply don’t seem to work, there is a good chance you don’t have mod_rewrite installed. Ask your tech support.
  • Mobile site in a subdirectory, eg. www.yourdomain.com/m/. This set of rules I’ve posted will not work with a mobile subdomain.

The Steps

  1. Download mobilerules2.1.txt.
  2. Open the file in your favorite text editor, replace the 2 instances of “mobiledirectoryhere” (without quotes) with the directory name you are using for your mobile site. Save it.
  3. Open FTP client, enable ‘view hidden files’ – files beginning with a dot are hidden on linux. This option is typically buried in a “view” menu or something to that effect. Results may vary.
  4. Navigate to your site’s webroot (probably ‘htdocs’ or ‘www’).
    • If there is already a .htaccess file present in the directory. Download it, open it in your favorite text editor. Copy & paste the contents ofmobilerules2.1.txt into the file. Save it, upload it.
    • If there is no .htaccess file present. Upload mobilerules2.1.txt as is, rename it to .htaccess.
  5. Your Done. Test it from a mobile device.

If you have any questions please leave a comment.

Note: This script treats all opera mini, iphone and android the same as other phones and browsers. Leave some comments if you’d like me to address this.

19 replies on “How To Detect Mobile Visitors Using .htaccess Rewrite Rules, Simplified”

Thank you Ryan, this script seems to be working fine. I have to add “RewriteEngine On” on op, right?

One question. Currently this script only seems to work when I visit http://mydomain/. Not when I go to http://mydomain/page2.html. How would I achieve that, when a mobile visitor goes to ANY page in the main directory, he gets redirected to the mobile subdirectory?

Thanks!
Pieter

Hello,
Thank you very much fr you script, it works fine but i have the same redirect problem, when i open any web page directly by entering it full address the redirect code not working. for example:

1)- if i typed: http://mywebsite.com/ it redirect automatically to: mywebsite.com/m/

2)- but if i typed: http://mywebsite.com/about_us.html – it wont go to http://mywebsite.com/m/about_us.html (i have already created that page under /m/ directory.

any suggestion will be highly appreciated…

I think it work now after i have made some changes to manage the redirection instead of adding to lines.

#redirect mobile browser using HTTP_ACCEPT header
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|opera” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “mini|nitro|j2me|midp-|cldc-|netfront|mot|up.browser|up.link|audiovox”[NC,OR]
RewriteCond %{HTTP_USER_AGENT} “blackberry|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]
RewriteCond %{HTTP_HOST} “!m.websiteurl.com” [NC]
RewriteRule (.*) http://m.websiteurl.com/$1 [L]

RewriteCond %{HTTP_USER_AGENT} “sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera” [NC,OR]

The “opera” string will effectively redirect versions of the desktop Opera browser to the mobile version – it would be better to use “opera mini|opera mobi” instead of it.

Hi Ryan,

Many thanks for posting this! I’ve used the php version, but that has always meant one needs to re-direct the main index page to index2 which doesn’t look nice in the PC browser.

My only question with using this script – is should a mobile user wish to see the “Normal” website…what can one do to allow this? With the PHP script it was easy as all I did was point the mobiuser to index2 page.

Is there anyway for a mobi user to look at the normal website?

Yes, this it’s possible for the user to view the normal site, it’s just a little more involved.

1. Create a link to “view normal site” that links to bypassmobile.php.
2. In the php file set a cookie, let’s call it “bypass”
3. In .htaccess, test if the cookie is set. If it’s not set, the conditions won’t be met:
RewriteCond %{HTTP_COOKIE} !^.*bypass.*$ [NC]

Hi Ryan,

Many thanks for this but somewhere I must have done something wrong. Pardon my inexperience with .htaccess coding! I’m self taught IT person who knows a little about everything! 😉

I tried to follow your instructions, but everytime I point my iPhone to Visit Main Site (bypass.php) – it routes me back to the mobisite?

Here is my PHP set cookie code:

Here is my .htaccess code:

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

Would you be able to point me in the right direction as to why it keeps going back to the mobisite.

Many thanks in advance for your kind assistance. Chris!

Hi Ryan,

Many thanks for this but somewhere I must have done something wrong. Pardon my inexperience with .htaccess coding! I’m self taught IT person who knows a little about everything! 😉

I tried to follow your instructions, but everytime I point my iPhone to Visit Main Site (bypass.php) – it routes me back to the mobisite?

Here is my PHP set cookie code:

Here is my .htaccess code:

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

Would you be able to point me in the right direction as to why it keeps going back to the mobisite.

Many thanks in advance for your kind assistance. Chris!

Hi Ryan,

Many thanks for this but somewhere I must have done something wrong. Pardon my inexperience with .htaccess coding! I’m self taught IT person who knows a little about everything! 😉

I tried to follow your instructions, but everytime I point my iPhone to Visit Main Site (bypass.php) – it routes me back to the mobisite?

Here is my PHP set cookie code:

/* set the cookie */
setcookie(“bypass”);
?>

Here is my .htaccess code:

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

Would you be able to point me in the right direction as to why it keeps going back to the mobisite.

Many thanks in advance for your kind assistance. Chris!

Hi Ryan,

Not to worry, I figured it out. My code below now works: with the bypass cookie.

Thanks

Chris

RewriteEngine On

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

Leave a Reply

Only people in my network can comment.