Since the massive rise of smartphones and tablets like the iPhone, iPad, Android phones and tablets, BlackBerries, etc. you might have considered creating a mobile version of your web site. This tutorial explains how to configure Apache to serve the mobile version of your web site if the visitor uses a mobile device, and the normal version if the visitor uses a normal desktop PC. This can be achieved with Apache's rewrite module.

I do not issue any guarantee that this will work for you!


1 Preliminary Note

In this tutorial, my "normal" web site is accessible under and, while my mobile site is called These vhosts already exist on my system, so I'm not going to cover how to set them up.


2 Enabling mod_rewrite

First you have to make sure that the Apache module mod_rewrite is enabled. On Debian/Ubuntu, you can enable it like this:

a2enmod rewrite

Restart Apache afterwards - for Debian/Ubuntu, the command is:

/etc/init.d/apache2 restart


3 Configuring Apache To Allow Rewrite Rules In .htaccess Files

My "normal" web site has the vhost configuration file /etc/apache2/sites-available/ and the document root /var/www/

My mobile site has the vhost configuration file /etc/apache2/sites-available/ and the document root /var/www/

I want to place the rewrite rules for each site in an .htaccess file (although it is also possible to place the rewrite rules directly in the vhost configuration file). Therefore I must first modify our vhost configurations so that both .htaccess files are allowed to contain rewrite directives. We can do this with the line AllowOverride All (which allows .htaccess to override all settings in the vhost configuration):

vi /etc/apache2/sites-available/

			        <Directory /var/www/>
			                AllowOverride All	</Directory>[...]			

vi /etc/apache2/sites-available/

			        <Directory /var/www/>
			AllowOverride All        </Directory>[...]			

Restart Apache afterwards:

/etc/init.d/apache2 restart


4 Creating Rewrite Rules

Now let's create the rewrite rules for the "normal" web site that will redirect all users of mobile devices to the mobile version - I focus on the relevant devices/user agents here which are Android, Blackberry, googlebot-mobile (Google's mobile search bot), IE Mobile, iPad, iPhone, iPod, Opera Mobile, PalmOS, and WebOS.

The /var/www/ file looks as follows:

vi /var/www/

						<IfModule mod_rewrite.c>RewriteEngine OnRewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]RewriteRule ^$ [L,R=302]</IfModule>			

For our mobile web site, the rewrite rules that redirect all users that don't use a mobile device to our "normal" web site look as follows - I've simply negated the RewriteCond condition from the previous .htaccess file:

vi /var/www/

						<IfModule mod_rewrite.c>RewriteEngine OnRewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]RewriteRule ^$ [L,R=302]</IfModule>			

That's it! Now you can do some testing, e.g. visit with a normal desktop browser:

If all goes well, you should be redirected to

Now test with a mobile device (I use an Android phone here) and go to

You should be redirected to