Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On Device Using mod_rewrite
2020-12-13 06:01
标签:des android style class c tar 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! 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. 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 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 vi /etc/apache2/sites-available/m.example.com.vhost Restart
Apache afterwards: /etc/init.d/apache2 restart 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 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 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: Apache2: How To Redirect Users To Mobile Or Normal Web Site
Based On Device Using mod_rewrite,搜素材,soscw.com Apache2: How To Redirect Users To Mobile Or Normal Web Site
Based On Device Using mod_rewrite 标签:des android style class c tar 原文地址:http://www.cnblogs.com/changye/p/3739001.htmlhttp://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
1
Preliminary Note
2
Enabling mod_rewrite
3
Configuring Apache To Allow Rewrite Rules In .htaccess Files
[...]
[...]
4
Creating Rewrite Rules
5
Links
文章标题:Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On Device Using mod_rewrite
文章链接:http://soscw.com/essay/32233.html