Dan*_*Dan 6 .htaccess mobile redirect
我已经使用下面的.htaccess代码为移动/桌面重定向提供了一个不错的工作解决方案,但是如果有人可以提供帮助,还有一些可以真正实现最终的增强功能.
目录设置:
Cookie启用链接:
当前.htaccess文件:
RewriteEngine on
# Check if this is the desktop to mobile query string
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
# Set a cookie, and skip the next 2 rules 
RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=2]
# Check if this is the mobile to desktop query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
RewriteRule ^(.*)$ http://m.website.com/ [L,R=302]
我想要添加的东西:
此外,如果有人注意到当前代码中的任何错误或改进,也会喜欢听到它!
RewriteEngine on
# Check if this is the desktop to mobile query string
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
# Set a cookie, and skip the next 2 rules 
RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=2]
# Check if this is the mobile to desktop query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
RewriteRule ^(.*)$ http://m.website.com/ [L,R=302]
# 1.) Check if on desktop site and having www., if not redirect to www.website.com
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteRule ^(.*)$ http://www.website.com$1 [R=301,L]
2.) 这不能用 mod_rewrite 来完成,除非“/chairs”是一个现有文件(我猜它不是)。所以我会用你想要的脚本语言来检查它 - 例如在 php 中:
if (!pageexists($_GET['page']) && $_SERVER['HTTP_HOST'] == 'm.website.com')
  header("Location: http://m.website.com/");
其中 pageexists() 是您定义的函数,用于检查移动版本中是否有该页面的内容。
| 归档时间: | 
 | 
| 查看次数: | 3278 次 | 
| 最近记录: |