Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On Device Using mod_rewrite

http://www.howtoforge.com/apache2-how-to-redirect-users-to-mobile-or-normal-web-site-based-on-device-using-mod_rewrite

Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On
Device Using mod_rewrite


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 http://www.example.com and http://example.com, while my mobile site is called http://m.example.com. 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 www.example.com/example.com has the vhost configuration file /etc/apache2/sites-available/www.example.com.vhost and the document root/var/www/www.example.com/web.

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

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/www.example.com.vhost




[...]
<Directory /var/www/www.example.com/web/>
AllowOverride All
</Directory>
[...]

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




[...]
<Directory /var/www/www.example.com/mobile/>
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 www.example.com/example.com that will redirect all users of
mobile devices to the mobile version m.example.com - 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/www.example.com/web/.htaccess file
looks as follows:

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




<IfModule mod_rewrite.c>
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]
</IfModule>

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

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




<IfModule mod_rewrite.c>
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]
</IfModule>

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

If
all goes well, you should be redirected to www.example.com:

Now
test with a mobile device (I use an Android phone here) and go to www.example.com:

You
should be redirected to m.example.com:

5
Links


Apache2: How To Redirect Users To Mobile Or Normal Web Site
Based On Device Using mod_rewrite,布布扣,bubuko.com

Apache2: How To Redirect Users To Mobile Or Normal Web Site
Based On Device Using mod_rewrite

时间: 2024-08-14 09:51:32

Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On Device Using mod_rewrite的相关文章

How to proxy a web site by apache2 in Ubuntu

Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we can find that the apache2 has been installed in "/etc/" directory. [email protected]:cd /etc/apache2 [email protected]:/etc/apache2$ apache2 -versi

[Tools] Using mobile device for debugging your mobile web site

1. First you have enable "Developer mode" on your mobile device. (Different device might be different) "Settings" -> Click "Build Number" 7 times. 2. Enable "USB debugging" "Settings" -> "Develo

ArcGIS10.2最新全套下载地址

http://www.tuicool.com/articles/VfaMfy 免责声明: 该链接来自于哥伦比亚大学或者牛津大学的网站链接, 下载 软件之前确保有正版的软件授权 ,本博客只是转载了网站链接而已,如果用户有相关的盗版行为,与本博客和本作者无关! 特此声明! ---------------------------------------------------------------------------------- 版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

Automating mobile web apps

Automating mobile web apps If you're interested in automating your web app in Mobile Safari on iOS or Chrome on Android, Appium can help you. Basically, you write a normal WebDriver test, and use Appium as the Selenium server with a special set of de

使用 jQuery Mobile 和 CSS3 实现响应式设计

使用 jQuery Mobile 和 CSS3 实现响应式设计 创建可以适应每个用户的屏幕分辨率的 Web 页面布局 jQuery Mobile 框架就其本身而言就是一个优秀的解决方案,可以快速创建一个可访问的由 HTML5 标记驱动的移动网站.当与 CSS3 配合使用时,工作真的会变得很有趣,因为它使创建一个根据屏幕分辨率来响应用户设备的 Web 页面布局成为可能.本文展示了如何使用 jQuery Mobile 框架和 CSS3 媒体查询在您的 Web 页面中实现响应式设计. jQuery M

[Amaze UI] 如何推进 mobile first 的前端 Web 方案

背景:云适配创立之初,我们就开始积累自己的前端框架,同时也借鉴了Bootstrap等国外框架的优点.在内部使用过程中,大家一致反映不错,我们就希望把这个产品开源,希望分享给更多的人,也希望更多的人来贡献代码,来共建中国前端开源生态环境. 由于Amaze UI目前处在内测期,我们希望能尽可能多的收集到优秀的.有建设性的反馈建议和看法,与广大优秀前端开发者共同完善Amaze UI 的功能,推动中国移动跨屏前端技术的发展.(内测地址:http://amazeui.org/landing) 下面提前跟大

[HTML5_JQueryMobile]20个很棒的 jQuery Mobile 教程

构建一个餐馆选择的 Web 应用 How to build a jQuery Mobile app for choosing a restaurant based on what the user want to eat tonight, the town where they want to eat and other user’s ratings of the restaurants. 创建一个良好移动体验应用 For larger data-focused sites, or sites

13 Reasons Why You Should Pay Attention to Mobile Web Performance

Mobile is no longer on the sidelines. If you’re not already thinking mobile first, you should at least consider it. Let’s go over compelling data that demonstrate the importance of focusing on performance for mobile devices. Business is Booming on th