使用.htaccess文件作为站点的移动版本

时间:2012-03-21 12:53:22

标签: .htaccess jquery-mobile user-agent

我做了一个jQuery移动网站。我想在原始站点的FTP中上传此目录。现在我只想检测移动设备中的移动网站。这就是为什么人们只能在移动设备上打开移动版本的原因。  就像我有我的网站url www.example.com,它应该在桌面网站上打开。  另一个网站意味着移动网站应该在www.example / mobile site上打开。

我找到了检测此类移动版本的文档

# don't apply the rules if you're already in the mobile directory, infintine loop
# you'll want to test against the host if you're using a subdomain
RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
# if the browser accepts these mime-types, it's definitely mobile, or pretending to be
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
# a bunch of user agent tests
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /mobiledirectoryhere/ [L,R=302]

我将“mobiledirectoryhere”更改为我的移动版网站目录“mobile”。之后,我将文件上传到FTP并将其重命名为.htaccess

我浏览了参考网站http://ohryan.ca/blog/2010/04/10/how-to-detect-mobile-visitors-using-htaccess-rewrite-rules-simplified/

1 个答案:

答案 0 :(得分:0)

我的网站检测很简单。只需将.htaccess文件添加到根目录,其中包含以下内容。

RewriteEngine on
RewriteCond %{HTTP_HOST} !^m\.myurlname\.com$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^/?(.*)$ http://m.myurlname.com/$1 [L,R=302]

然后保存。

相关问题