Redirect http users to mobile site using Apache mod_rewrite via user-agent detection

It’s getting important now to have websites that display good on mobile devices, and according to the statistics more than 85% smartphone owners access the internet on their mobile devices. There are a lot of ways to build mobile sites and also few options how to serve these to your mobile users. There are 2 popular ways to do it:

1. detects the http user-agent and if matches a mobile user-agent, then visitors will see a mobile version of the website. Normally, the easiest way to do it by installing mobile theme plugins especially for WordPress users. URLs or domain name will be the same for desktop and mobile visitors, and they have an option to swtich between the regular or mobile theme websites.

2. detects the http user-agent and then redirect to a different page, either to a subdomain, top level domain or directories. But the most common one is to place them to a subdomain, like m.freelinuxtutorials.com

Below is a procedure on how to redirect users to your mobile site

desktop or normal site: www.freelinuxtutorials and freelinuxtutorials.com
mobile site: m.freelinuxtutorials.com

If it is hosted on the same server on single IP, then you have to create a subdomain pointed to same A record, then configure your vhosts.

Tested on CentOS/RHEL

1. Enable the Apache mod_rewrite module. Under /etc/httpd/conf/httpd.conf, look for this line and make sure is not commented out.

LoadModule rewrite_module modules/mod_rewrite.so

2. Configure Apache Vhosts and RewriteCond. I will not cover so much on this but basically this is to show a sample configuration


DocumentRoot /var/www/html/freelinux
ServerName freelinuxtutorials.com
ServerAlias www.freelinuxtutorials.com

RewriteEngine On

RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* – [F]

RewriteCond %{HTTP_USER_AGENT} Access.* [OR]
RewriteCond %{HTTP_USER_AGENT} Acer.* [OR]
RewriteCond %{HTTP_USER_AGENT} Aiko.* [OR]
RewriteCond %{HTTP_USER_AGENT} Airness.* [OR]
RewriteCond %{HTTP_USER_AGENT} Alcatel.* [OR]
RewriteCond %{HTTP_USER_AGENT} AlphaCell.* [OR]
RewriteCond %{HTTP_USER_AGENT} Amazon.* [OR]
RewriteCond %{HTTP_USER_AGENT} Amoi.* [OR]
RewriteCond %{HTTP_USER_AGENT} Amoisonic.* [OR]
RewriteCond %{HTTP_USER_AGENT} Android.* [OR]
RewriteCond %{HTTP_USER_AGENT} AnexTek.* [OR]
RewriteCond %{HTTP_USER_AGENT} Asmobile.* [OR]
RewriteCond %{HTTP_USER_AGENT} Aspen.* [OR]
RewriteCond %{HTTP_USER_AGENT} Asus.* [OR]
RewriteCond %{HTTP_USER_AGENT} Audiovox.* [OR]
RewriteCond %{HTTP_USER_AGENT} BEKO.* [OR]
RewriteCond %{HTTP_USER_AGENT} BenQ.* [OR]
RewriteCond %{HTTP_USER_AGENT} BlackBerry.* [OR]
RewriteCond %{HTTP_USER_AGENT} BlackBerry8300.* [OR]
RewriteCond %{HTTP_USER_AGENT} BenQ-Siemens.* [OR]
RewriteCond %{HTTP_USER_AGENT} BenQSiemens.* [OR]
RewriteCond %{HTTP_USER_AGENT} Bird.* [OR]
RewriteCond %{HTTP_USER_AGENT} Bytemobile.* [OR]
RewriteCond %{HTTP_USER_AGENT} CDM.* [OR]
RewriteCond %{HTTP_USER_AGENT} CECT.* [OR]
RewriteCond %{HTTP_USER_AGENT} Capitel.* [OR]
RewriteCond %{HTTP_USER_AGENT} CheckCom.* [OR]
RewriteCond %{HTTP_USER_AGENT} Compaq.* [OR]
RewriteCond %{HTTP_USER_AGENT} DBTEL.* [OR]
RewriteCond %{HTTP_USER_AGENT} Dai.* [OR]
RewriteCond %{HTTP_USER_AGENT} Dallab.* [OR]
RewriteCond %{HTTP_USER_AGENT} Danger.* [OR]
RewriteCond %{HTTP_USER_AGENT} Dmobo.* [OR]
RewriteCond %{HTTP_USER_AGENT} Dopod.* [OR]
RewriteCond %{HTTP_USER_AGENT} DreamPhone.* [OR]
RewriteCond %{HTTP_USER_AGENT} ELSON.* [OR]
RewriteCond %{HTTP_USER_AGENT} ETEN.* [OR]
RewriteCond %{HTTP_USER_AGENT} EZIO.* [OR]
RewriteCond %{HTTP_USER_AGENT} Elite.* [OR]
RewriteCond %{HTTP_USER_AGENT} Emblaze.* [OR]
RewriteCond %{HTTP_USER_AGENT} Enteos.* [OR]
RewriteCond %{HTTP_USER_AGENT} Ericsson.* [OR]
RewriteCond %{HTTP_USER_AGENT} Ezze.* [OR]
RewriteCond %{HTTP_USER_AGENT} Fly.* [OR]
RewriteCond %{HTTP_USER_AGENT} Mobile.* [OR]
RewriteCond %{HTTP_USER_AGENT} Gigabyte.* [OR]
RewriteCond %{HTTP_USER_AGENT} Google.* [OR]
RewriteCond %{HTTP_USER_AGENT} Gradiente.* [OR]
RewriteCond %{HTTP_USER_AGENT} Grundig.* [OR]
RewriteCond %{HTTP_USER_AGENT} HP.* [OR]
RewriteCond %{HTTP_USER_AGENT} HTC.* [OR]
RewriteCond %{HTTP_USER_AGENT} Haier.* [OR]
RewriteCond %{HTTP_USER_AGENT} Handspring.* [OR]
RewriteCond %{HTTP_USER_AGENT} Helio.* [OR]
RewriteCond %{HTTP_USER_AGENT} Hewlett-Packard.* [OR]
RewriteCond %{HTTP_USER_AGENT} Hitachi.* [OR]
RewriteCond %{HTTP_USER_AGENT} Huawei.* [OR]
RewriteCond %{HTTP_USER_AGENT} Hummer.* [OR]
RewriteCond %{HTTP_USER_AGENT} IXI.* [OR]
RewriteCond %{HTTP_USER_AGENT} Imate.* [OR]
RewriteCond %{HTTP_USER_AGENT} Innostream.* [OR]
RewriteCond %{HTTP_USER_AGENT} Itelco.* [OR]
RewriteCond %{HTTP_USER_AGENT} iPhone.* [OR]
RewriteCond %{HTTP_USER_AGENT} itouch.* [OR]
RewriteCond %{HTTP_USER_AGENT} KDDI.* [OR]
RewriteCond %{HTTP_USER_AGENT} KONKA.* [OR]
RewriteCond %{HTTP_USER_AGENT} KPT.* [OR]
RewriteCond %{HTTP_USER_AGENT} Kejian.* [OR]
RewriteCond %{HTTP_USER_AGENT} Klondike.* [OR]
RewriteCond %{HTTP_USER_AGENT} Kyocera.* [OR]
RewriteCond %{HTTP_USER_AGENT} LG.* [OR]
RewriteCond %{HTTP_USER_AGENT} Lenovo.* [OR]
RewriteCond %{HTTP_USER_AGENT} Lexibook.* [OR]
RewriteCond %{HTTP_USER_AGENT} Lobster.* [OR]
RewriteCond %{HTTP_USER_AGENT} MOMO.* [OR]
RewriteCond %{HTTP_USER_AGENT} Maxon.* [OR]
RewriteCond %{HTTP_USER_AGENT} Medion.* [OR]
RewriteCond %{HTTP_USER_AGENT} MiTAC.* [OR]
RewriteCond %{HTTP_USER_AGENT} Mio.* [OR]
RewriteCond %{HTTP_USER_AGENT} Miracle.* [OR]
RewriteCond %{HTTP_USER_AGENT} Mitsubishi.* [OR]
RewriteCond %{HTTP_USER_AGENT} Modelabs.* [OR]
RewriteCond %{HTTP_USER_AGENT} Motorola.* [OR]
RewriteCond %{HTTP_USER_AGENT} NEC.* [OR]
RewriteCond %{HTTP_USER_AGENT} NTT.* [OR]
RewriteCond %{HTTP_USER_AGENT} Neonode.* [OR]
RewriteCond %{HTTP_USER_AGENT} Newgen.* [OR]
RewriteCond %{HTTP_USER_AGENT} Nintendo.* [OR]
RewriteCond %{HTTP_USER_AGENT} Nokia.* [OR]
RewriteCond %{HTTP_USER_AGENT} Novarra.* [OR]
RewriteCond %{HTTP_USER_AGENT} O2.* [OR]
RewriteCond %{HTTP_USER_AGENT} ONDA.* [OR]
RewriteCond %{HTTP_USER_AGENT} Openwave.* [OR]
RewriteCond %{HTTP_USER_AGENT} Opera.* [OR]
RewriteCond %{HTTP_USER_AGENT} Orange.* [OR]
RewriteCond %{HTTP_USER_AGENT} Palm.* [OR]
RewriteCond %{HTTP_USER_AGENT} PalmOne.* [OR]
RewriteCond %{HTTP_USER_AGENT} Panasonic.* [OR]
RewriteCond %{HTTP_USER_AGENT} Pantech.* [OR]
RewriteCond %{HTTP_USER_AGENT} Philips.* [OR]
RewriteCond %{HTTP_USER_AGENT} Pirelli-Arcor.* [OR]
RewriteCond %{HTTP_USER_AGENT} Porshe.* [OR]
RewriteCond %{HTTP_USER_AGENT} Psion.* [OR]
RewriteCond %{HTTP_USER_AGENT} QCI.* [OR]
RewriteCond %{HTTP_USER_AGENT} Qtek.* [OR]
RewriteCond %{HTTP_USER_AGENT} RIM.* [OR]
RewriteCond %{HTTP_USER_AGENT} Reporo.* [OR]
RewriteCond %{HTTP_USER_AGENT} SKYZEN.* [OR]
RewriteCond %{HTTP_USER_AGENT} SPV.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sagem.* [OR]
RewriteCond %{HTTP_USER_AGENT} Samsung.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sanyo.* [OR]
RewriteCond %{HTTP_USER_AGENT} SavaJe.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sendo.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sharp.* [OR]
RewriteCond %{HTTP_USER_AGENT} Siemens.* [OR]
RewriteCond %{HTTP_USER_AGENT} Skyspring.* [OR]
RewriteCond %{HTTP_USER_AGENT} SmartTrust.* [OR]
RewriteCond %{HTTP_USER_AGENT} Smile.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sonim.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sony.* [OR]
RewriteCond %{HTTP_USER_AGENT} Spice.* [OR]
RewriteCond %{HTTP_USER_AGENT} Sunrise.* [OR]
RewriteCond %{HTTP_USER_AGENT} T-Mobile.* [OR]
RewriteCond %{HTTP_USER_AGENT} TCL.* [OR]
RewriteCond %{HTTP_USER_AGENT} TIANYU.* [OR]
RewriteCond %{HTTP_USER_AGENT} TTPCom.* [OR]
RewriteCond %{HTTP_USER_AGENT} Telit.* [OR]
RewriteCond %{HTTP_USER_AGENT} Toplux.* [OR]
RewriteCond %{HTTP_USER_AGENT} Toshiba.* [OR]
RewriteCond %{HTTP_USER_AGENT} UCWEB.* [OR]
RewriteCond %{HTTP_USER_AGENT} UTStarcom.* [OR]
RewriteCond %{HTTP_USER_AGENT} Uniscope.* [OR]
RewriteCond %{HTTP_USER_AGENT} Unistar.* [OR]
RewriteCond %{HTTP_USER_AGENT} Uriver.* [OR]
RewriteCond %{HTTP_USER_AGENT} Utec.* [OR]
RewriteCond %{HTTP_USER_AGENT} V3m.* [OR]
RewriteCond %{HTTP_USER_AGENT} VITELCOM.* [OR]
RewriteCond %{HTTP_USER_AGENT} VK.* [OR]
RewriteCond %{HTTP_USER_AGENT} VODAFONE.* [OR]
RewriteCond %{HTTP_USER_AGENT} Verizon.* [OR]
RewriteCond %{HTTP_USER_AGENT} Vertu.* [OR]
RewriteCond %{HTTP_USER_AGENT} Vitelcom.* [OR]
RewriteCond %{HTTP_USER_AGENT} Voxtel.* [OR]
RewriteCond %{HTTP_USER_AGENT} W3C.* [OR]
RewriteCond %{HTTP_USER_AGENT} WAPUniverse.* [OR]
RewriteCond %{HTTP_USER_AGENT} Wapsilon.* [OR]
RewriteCond %{HTTP_USER_AGENT} WinWAP.* [OR]
RewriteCond %{HTTP_USER_AGENT} Wonu.* [OR]
RewriteCond %{HTTP_USER_AGENT} XDA.* [OR]
RewriteCond %{HTTP_USER_AGENT} Xda.* [OR]
RewriteCond %{HTTP_USER_AGENT} Yas.* [OR]
RewriteCond %{HTTP_USER_AGENT} Symbian.* [OR]
RewriteCond %{HTTP_USER_AGENT} MOT-V3r.* [OR]
RewriteCond %{HTTP_USER_AGENT} Yospace.* [OR]
RewriteCond %{HTTP_USER_AGENT} ZTE.* [OR]
RewriteCond %{HTTP_USER_AGENT} Zonda.* [OR]
RewriteCond %{HTTP_USER_AGENT} i-mate.* [OR]
RewriteCond %{HTTP_USER_AGENT} iKoMo.* [OR]
RewriteCond %{HTTP_USER_AGENT} tvCompass.*
RewriteRule ^(.+)$ http://m.freelinuxtutorials.com [L]


DocumentRoot “/var/www/html/freelinuxmobile”
ServerName m.freelinuxtutorials.com

3. Restart or reload apache service to apply changes

[root@localhost conf]# service httpd reload
Reloading httpd: [ OK ]

Quick tip:
Simple way to do this is:

RewriteCond %{HTTP_USER_AGENT} “android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos” [NC]
RewriteRule ^(.+)$ http://m.freelinuxtutorials.com/ [L]

About the author

Free Linux

View all posts

2 Comments

  • Good post. I learn something new and challenging on websites
    I stumbleupon everyday. It will always be interesting to read
    content from other writers and use something from other websites.

  • Thanks Lily for dropping by. More power!

Leave a Reply