2017-06-29 68 views
1

我想拿出一个htaccess的代码,将允许我重定向用户在所有移动设备去网页开始www.example.com/blogexample.com/blog到特定页面 - 几乎与以下扩展名相同的链接:/mobile添加'/移动'到移动设备上的网址

使移动设备的网站成为:

www.example.com/blog -> www.example.com/blog/mobile 

example.com/blog -> example.com/blog/mobile 

我怎么能这样做?

回答

1

你可以把这个规则在站点根的.htaccess:

RewriteEngine On 

RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ 
RewriteRule ^blog/?$ /$0/mobile [L,NC,R=302] 

这是假设你没有.htaccessblog/目录。

如果你已经有了一个blog/.htaccess然后使用这个规则blog/.htaccess

RewriteEngine On 

RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ 
RewriteRule ^/?$ /blog/mobile [L,R=302] 

如果上面标头不工作,然后用user agent基于检查:

RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR] 
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] 
RewriteRule ^blog/?$ /$0/mobile [L,NC,R=302] 
+0

如何可靠的是'用于检测“移动设备”的“x-wap-profile”或“Profile”标题?我的移动设备都没有发送任何这些头文件? – MrWhite

+1

好吧然后第二个选项是使用'HTTP_USER_AGENT'条件在我编辑的答案。 – anubhava