终极移动.htaccess重定向

时间:2011-11-24 02:23:59

标签: .htaccess redirect mobile

我已经使用下面的.htaccess代码为移动/桌面重定向提供了一个不错的工作解决方案,但是如果有人可以提供帮助,还有一些其他功能可以真正做到最终。

目录设置:

  • 桌面版网站:website.com
  • 移动网站:m.website.com

启用Cookie的链接:

  • 从桌面网站迁移到移动网站,将Cookie设置为移动设备:http://m.website.com?m = 1
  • 从移动网站转到桌面网站,将Cookie设置为停留在桌面上:http://www.website.com?m = 0

当前.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]

我想要添加的内容:

  1. 如果我们要使用移动设备或只是普通的桌面访问桌面版,请确保它始终添加www。
  2. 对于桌面到移动重定向,直接匹配移动页面,但如果不存在,则默认为移动主页。 (例如:如果您在移动设备上访问website.com/chairs,它将尝试重定向到m.website.com/chairs,但如果它不存在,它将改为重定向到m.website.com。我认为映射会类似于^(。*)$ http://m.website.com/$1,但不确定如何实现回退到主页
  3. 此外,如果有人注意到当前代码中的任何错误或改进,也会喜欢听到它!

2 个答案:

答案 0 :(得分:0)

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()是您定义的函数,用于检查移动版本中是否有此页面的内容。

答案 1 :(得分:0)

RewriteEngine On RewriteBase /

RewriteCond%{HTTP_USER_AGENT}(android | bb \ d + | meego)。+ mobile | avantgo | bada / | blackberry | blazer | compal | elaine | fennec | hiptop | iemobile | ip(hone | od)| iris | kindle | lge \ | maemo | midp | mmp | netfront | opera \ m(ob | in)i | palm(\ os)?| phone | p(ixi | re)/ | plucker | pocket | psp | series(4 | 6 )0 | symbian | treo | up。(浏览器|链接)| vodafone | wap | windows \(ce | phone)| xda | xiino [NC,OR] RewriteCond%{HTTP_USER_AGENT} ^(1207 | 6310 | 6590 | 3gso | 4thp | 50 [1-6] i | 770s | 802s | a \ wa | abac | ac(er | oo | s - )| ai(ko | rn )| al(av | ca | co)| amoi | an(ex | ny | yw)| aptu | ar(ch | go)| as(te | us)| attw | au(di | -m | r \ | s \)| avan | be(ck | ll | nq)| bi(lb | rd)| bl(ac | az)| br(e | v)w | bumb | bw-(n | u)| c55 / | CAPI | CCWA | CDM-|细胞| CHTM | CLDC | CMD- |ω(MP | ND)|嗉|大(它| LL | NG)| dbte | DC-S |德维| DICA | dmob |做(C | p)Ô| DS(12 | -d)| EL(49 | AI)| EM(L2 | UL)| ER(IC | K0)| esl8 | EZ([4-7] 0 | OS | WA | ZE) | fetc | fly( - | )| g1 \ u | g560 | gene | gf-5 | g-mo | go(.w | od)| gr(ad | un)| haie | hcit | hd-( m | p | t)| hei- | hi(pt | ta)| hp(\ i | ip)| hs-c | ht(c( - | \ | | a | g | p | s | t)| tp)| hu(aw | tc)| i-(20 | go | ma)| i230 | iac(\ | - | /)| ibro | idea | ig01 | ikom | im1k | inno | ipaq | iris | ja(t | v)a | jbro | jemu | jigs | kddi | keji | kgt(\ | /)| klon | kpt \ | kwc- | kyo(c | k)| le(no | xi)| lg(\克| /(K | L | U)| 50 | 54 | - [AW])| libw |山猫| M1-W |空间M3GA | M50 / |毫安(TE | UI | XO)| MC(01 | 21 | CA )| m-cr | me(rc | ri)| mi(o8 | oa | ts)| mmef | mo(01 | 02 | bi | de | do | t( - | \ | o | v)| zz)| mt(50 | p1 | v \)| mwbp | mywa | n10 [0-2] | n20 [2-3] | n30(0 | 2)| n50(0 | 2 | 5)| n7(0(0 | 1)| 10)| NE((C | M) - |在| TF | WF | WG |重量)| NOK(6 | I)| nzph | o2im | OP(TI | WV )|奥兰| owg1 | P800 |锅(A | d | T)| pdxg | PG(13 | - ([1-8] | C))|菲尔| PIRE | PL(AY | UC)| PN-2 |婆(CK |室温| SE)| PROX | PSIO | PT-G | QA-A | QC(07 | 12 | 21 | 32 | 60 | - [2-7] |异)| QTEK | R380 | R600 | RAKS | rim9 | RO(VE | ZO)| S55 / | SA(GE |毫安|毫米|毫秒| NY | VA)| SC(01 | H- | oo的|对 - )| SDK / | SE(C( - | 0 | 1)| 47 | MC | ND | RI)| SGH- |沙皮| SIE( - | M)| SK-0 | SL(45 | ID)| SM(AL | AR | B3 |它| T5) | so(ft | ny)| sp(01 | h- | v- | v \)| sy(01 | mb)| t2(18 | 50)| t6(00 | 10 | 18)| ta(gt | lk )| tcl- | tdg- |电话(I | M)|做为计时| T型钼|至(PL | SH)| TS(70 |间 - |立方米| M5)| TX-9 |向上(.B | G1 | SI)| utst | V400 | V750 | VERI | VI(RG | TE)| VK(40 | 5 [0-3] | -v)| vm40 |沃达| vulc | VX(52 | 53 | 60 | 61 | 70 | 80 | 81 | 83 | 85 | 98)| w3c( - | \)| webc | whit | wi(g \ | nc | nw)| wmlb | wonu | x700 | yas- | your | zeto | zte- )[NC] RewriteRule ^ $ http://detectmobilebrowser.com/mobile [R,L]