Using Apache And mod_rewrite to Redirect your Mobile User

While I'm browsing using my K800i, very often that I've accidentally visit a non-mobile site with huge flash or huge content. That sucks, that costs, since mobile internet is rather expensive in my location. So after searching and testing, I've shared a simple hacks for your existing blog or web application to redirect mobile user that visiting your website to mobile-specific pages.

The hacks is for apache webserver only, but nginx or lighttpd server can use the same concept using their config syntax. Put the following config snippet on your website either using .htaccess or directy editing the apache config.

RewriteEngine On

#redirect mobile browser using HTTP_ACCEPT header
RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC]
RewriteCond %{HTTP_HOST} "!m.yourmobileurl.com" [NC]
RewriteRule (.*) http://m.yourmobileurl.com/$1 [L]

#some high-end phone sometimes support HTML, only its sucks
#add more browser user agent sig here
RewriteCond %{HTTP_USER_AGENT} (nokia|symbian|iphone|blackberry) [NC] 
RewriteCond %{HTTP_HOST} "!m.yourmobileurl.com" [NC]
RewriteRule (.*) http://m.yourmobileurl.com/$1 [L]

Using HTTP_ACCEPT header is much more simple rather than hitting the HTTP_USER_AGENT variables. I guess there's the end of it, Have fun!

7 Comments | [Put comments]

  1. gravatar idel fuschini - June 4, 2009

    Today I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
    Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.

    Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

  2. gravatar ferdhie - June 4, 2009

    this is really a great info, thanks ..

  3. gravatar idel fuschini - November 16, 2009

    From 7th of october 2009 the "Apache Mobile Filter" is one of apache module included in the "Apache Module Registry" http://modules.apache.org/search.php?id=1787

  4. gravatar idel - March 13, 2010

    Last but not the least info, I have done this site where I test the module:
    http://www.apachemobilefilter.org

  5. gravatar sukeerthi adiga - April 26, 2011

    Coolest one

  6. gravatar idel - February 1, 2012

    I think the best way to redirect mobile is with "Apache Mobile Filter" an open source and free project below an example:

    PerlSetEnv AMFMobileHome /usr/local/AMF
    PerlSetEnv AMFProductionMode true
    PerlSetEnv ServerMemCached localhost:11211
    PerlTransHandler +Apache2::AMFLiteDetectionFilter
    RewriteEngine on
    RewriteCond %{ENV:AMF_ISMOBILE} ^true*
    RewriteRule ^(.*)$ http://m.foo.org [R=301,L]
    you can also use AMFDetectRightFilter, AMF51DegreesFilter or AMFWURFLFilter is depend of which device repository you want to use.

    For more detail: http://www.apachemobilefilter.org

  7. gravatar backlink solutions - February 12, 2012

    hi www.ferdianto.com owner discovered your site via Google but it was hard to find and I see you could have more visitors because there are not so many comments yet. I have found site which offer to dramatically increase traffic to your blog http://xrumerservice.org they claim they managed to get close to 1000 visitors/day using their services you could also get lot more targeted traffic from search engines as you have now. I used their services and got significantly more visitors to my site. Hope this helps :) They offer most cost effective <a href=http://xrumerservice.org>backlinks</a> Take care. Jason

Leave a Reply