Apache2: How to Redirect Users to Mobile Or Normal Web Site Based on Device Using mod_rewrite

In the last few years, people have increasingly started using smartphones and tablets like the iPhone, iPad, Android phones, and Blackberries etc. And with this massive increase, some people have started creating a mobile version of their websites. This step by step guide helps you to configure Apache in order to run the mobile version of your website if the visitor accesses that website using a mobile device, and the normal version for those visitors who use their personal computers. The whole process can be done with the help of Apache’s rewrite module.

In this guide, we have created a website for desktop users, and it is accessible under http://www.example.com and http://example.com, and for mobile users our site name is http://m.example.com.

Instructions

  • 1

    Enabling mod_rewrite

    First of all you have to verify that Apache module mod_rewrite is enabled or not. In order to enable it on Debian/Ubuntu, type the following command;

    a2enmod rewrite

    Then restart Apache – for Debian/Ubuntu, the command is;

    /etc/init.d/apache2 restart

  • 2

    Configuring Apache to allow Rewrite rules in .htaccess wp-content/uploads

    The website which is created for desktop users – www.example.com / example.com – contains the vhost configuration file /etc/apache2/sites-available/m.example.com.vhost and the document root /var/www/www.example.com/mobile.

    Now, you have to put the rewrite rules in every site in an .htaccess file. To do this, you have to modify your vhost configurations so that both .htaccess wp-content/uploads can include rewrite directives. You can do this with the line AllowOverride All.

    vi /etc/apache2/sites-available/www.example.com.vhost

    [...]

    AllowOverride All

    [...]

    vi /etc/apache2/sites-available/m.example.com.vhost

    [...]

    AllowOverride All

    [...]

    Then restart Apache.

    /etc/init.d/apache2 restart

  • 3

    Creating Rewrite Rules

    Now you have make the rewrite rules for the website you have created for desktop users i.e www.example.com / example.com, and it will redirect all those users who are accessing it through their mobile devices to the mobile version m.example.com.

    The /var/www/www.example.com/web/.htaccess file looks as follows:

    vi /var/www/www.example.com/web/.htaccess

    RewriteEngine On

    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]

    RewriteRule ^$ http://m.example.com/ [L,R=302]

    For the mobile website m.example.com, the rewrite rules that redirect all users who access the website – www.example.com / example.com from desktop computers look as follow;

    vi /var/www/www.example.com/mobile/.htaccess

    RewriteEngine On

    RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]

    RewriteRule ^$ http://www.example.com/ [L,R=302]

  • 4

    All done. Now you can examine it by visiting m.example.com from a normal desktop.

  • 5

    If it is giving perfect result then redirect to www.example.com;

  • 6

    Now test your mobile device and visit www.example.com;

  • 7

    You will be redirected to m.example.com;

Leave a Reply

Your email address will not be published. Required fields are marked *


one + 4 =